-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
40 lines (32 loc) · 807 Bytes
/
client.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
package WebsocketServer
import (
"fmt"
"log"
"github.com/gorilla/websocket"
)
type client struct {
ID string
Conn *websocket.Conn
}
type message struct {
Type int `json:"type"`
Body string `json:"body"`
ClientID string
}
//Function to continously read incoming messages from the client in real time.
func (c *client) processMessage(ws *WebsocketServer){
defer func() {
ws.Unregister <- c
c.Conn.Close()
}()
for {
messageType, messageBody, err := c.Conn.ReadMessage()
if err != nil {
log.Println(err)
return
}
message := message{Type: messageType, Body: string(messageBody), ClientID: c.ID}
ws.Broadcast <- message
fmt.Printf("Message Received: %+v\n", message)
}
}