Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-is-dev committed May 7, 2021
0 parents commit 6025e73
Show file tree
Hide file tree
Showing 48 changed files with 30,258 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.crt
*.key
Binary file added backend.lnk
Binary file not shown.
9 changes: 9 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:1.11.1-alpine3.8
RUN apk add bash ca-certificates git gcc g++ libc-dev
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go mod download
RUN go build -v
RUN ls
CMD ["/app/realtime-chat-go-react"]
3 changes: 3 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/TutorialEdge/realtime-chat-go-react

require github.com/gorilla/websocket v1.4.0
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
3 changes: 3 additions & 0 deletions backend/launch REACT.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
@Title DarkRod Chat
@go run main.go
39 changes: 39 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"net/http"

"github.com/TutorialEdge/realtime-chat-go-react/pkg/websocket"
)

func serveWs(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
fmt.Println("WebSocket Endpoint Hit")
conn, err := websocket.Upgrade(w, r)
if err != nil {
fmt.Fprintf(w, "%+v\n", err)
}

client := &websocket.Client{
Conn: conn,
Pool: pool,
}

pool.Register <- client
client.Read()
}

func setupRoutes() {
pool := websocket.NewPool()
go pool.Start()

http.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
serveWs(pool, w, r)
})
}

func main() {
fmt.Println("DarkRod Chat App v1.0")
setupRoutes()
http.ListenAndServeTLS(":8080","cert.crt","cert.key",nil)
}
3 changes: 3 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions backend/pkg/websocket/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package websocket

import (
"encoding/json"
"fmt"
"log"
"sync"
// "time"
// "strconv"

"github.com/gorilla/websocket"
)

type Client struct {
ID string
Pseudo string
Conn *websocket.Conn
Pool *Pool
mu sync.Mutex
}

type Message struct {
Type int `json:"type"`
Body string `json:"body"`
}

type Message2 struct {
Type string
Body string
}
var message2 Message2

func (c *Client) Read() {

defer func() {
c.Pool.Unregister <- c
c.Conn.Close()
}()

for {
messageType, p, err := c.Conn.ReadMessage()
// fmt.Println(p)
if err != nil {
log.Println(err)
return
}

err2 := json.Unmarshal(p, &message2)
if err2 != nil {
log.Println(err2)
return
}

if message2.Type == "pseudo" {
c.Pseudo = message2.Body
} else if message2.Type == "msg" {
// fmt.Printf(message2.Body)
message := Message{Type: messageType, Body: "[TIME] : " + c.Pseudo + " : " + string(message2.Body)}
c.Pool.Broadcast <- message
fmt.Printf("Message Received: %+v\n", message)
}
}
}
64 changes: 64 additions & 0 deletions backend/pkg/websocket/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package websocket

import (
"fmt"
"strconv"

)

type Pool struct {
Register chan *Client
Unregister chan *Client
Clients map[*Client]bool
Broadcast chan Message
}

func NewPool() *Pool {
return &Pool{
Register: make(chan *Client),
Unregister: make(chan *Client),
Clients: make(map[*Client]bool),
Broadcast: make(chan Message),
}
}

func (pool *Pool) Start() {

p := fmt.Println

for {
select {
case client := <-pool.Register:
pool.Clients[client] = true
p("Size of Connection Pool: ", len(pool.Clients))
for client, _ := range pool.Clients {
client.Conn.WriteJSON(Message{Type: 2, Body: strconv.Itoa(len(pool.Clients)) })

p("Nouveau: ",client)
client.Conn.WriteJSON(Message{Type: 1, Body: "Nouveau utilisateur connecté... "})
}
break

case client := <-pool.Unregister:
p("Déconnecté: ",client)
delete(pool.Clients, client)
p("Size of Connection Pool: ", len(pool.Clients))
for client, _ := range pool.Clients {
client.Conn.WriteJSON(Message{Type: 2, Body: strconv.Itoa(len(pool.Clients)) })

client.Conn.WriteJSON(Message{Type: 1, Body: "Utilisateur déconnecté... "})
}
break

case message := <-pool.Broadcast:
p("Sending message to all clients in Pool")

for client, _ := range pool.Clients {
if err := client.Conn.WriteJSON(message); err != nil {
p(err)
return
}
}
}
}
}
24 changes: 24 additions & 0 deletions backend/pkg/websocket/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package websocket

import (
"log"
"net/http"

"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func Upgrade(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return nil, err
}

return conn, nil
}
Binary file added frontend.lnk
Binary file not shown.
23 changes: 23 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
44 changes: 44 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
1 change: 1 addition & 0 deletions frontend/launch REACT.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm start
Loading

0 comments on commit 6025e73

Please sign in to comment.