-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (116 loc) · 2.74 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
// initialize Hub
type Hub struct {
connections map[*Connection]bool
broadcast chan []byte
createConnection chan *Connection
destroyConnection chan *Connection
}
// initialize connections
type Connection struct {
hub *Hub
conn *websocket.Conn
send chan []byte
}
func main() {
hub := newHub()
go hub.launch()
// websocket handler
http.HandleFunc("/socket", func(w http.ResponseWriter, r *http.Request) {
socketChat(hub, w, r)
})
// index file handler
http.Handle("/", http.FileServer(http.Dir("./public")))
// start server
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic("Error: " + err.Error())
}
}
// creates a new hub object when called
func newHub() *Hub {
return &Hub{
broadcast: make(chan []byte),
createConnection: make(chan *Connection),
destroyConnection: make(chan *Connection),
connections: make(map[*Connection]bool),
}
}
// runs a goroutine for Hub when called
func (hub *Hub) launch() {
for {
select {
// receive connections from client and pass to hub to store
case connection := <-hub.createConnection:
hub.connections[connection] = true
// receive disconnects from client and pass to hub to delete
case connection := <-hub.destroyConnection:
if _, ok := hub.connections[connection]; ok {
delete(hub.connections, connection)
close(connection.send)
}
// receive messages from client and pass to hub to broadcast
case message := <-hub.broadcast:
for connection := range hub.connections {
select {
case connection.send <- message:
default:
close(connection.send)
delete(hub.connections, connection)
}
}
}
}
}
// write messages to server (goroutine)
func (c *Connection) writer() {
defer func() {
c.conn.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
c.conn.WriteMessage(websocket.TextMessage, message)
}
}
}
// read messages from server (goroutine)
func (c *Connection) reader() {
defer func() {
c.hub.destroyConnection <- c
c.conn.Close()
}()
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
c.hub.destroyConnection <- c
c.conn.Close()
break
}
c.hub.broadcast <- message
}
}
func socketChat(hub *Hub, w http.ResponseWriter, r *http.Request) {
// upgrade the connection
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// new Connection object
connection := &Connection{hub: hub, conn: conn, send: make(chan []byte)}
hub.createConnection <- connection
go connection.reader()
go connection.writer()
}