-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (37 loc) · 1.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"github.com/gorilla/websocket"
"net/http"
"fmt"
"chat/utils"
)
type Message struct {
Sender string
Recipient string
Content string
}
// In Go, top-level variable assignments must be prefixed with the var keyword. Omitting the var keyword is only allowed within blocks.
var room = ChatRoom{
clients: make(map[*Client]bool),
newClient: make(chan *Client),
broadcast: make(chan []byte),
}
func main() {
go room.start()
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
})
http.HandleFunc("/ws", handleWebSocketCommunication)
http.ListenAndServe(":5000", nil)
}
func handleWebSocketCommunication(res http.ResponseWriter, req *http.Request) {
uuid := utils.GenerateUUID(32)
conn, err := (&websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}).Upgrade(res, req, nil)
if err != nil {
fmt.Printf("something bad happened")
return
}
client := &Client{id: uuid, socket: conn, send: make(chan []byte)}
room.newClient <- client
go client.write()
go client.read()
}