-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
156 lines (138 loc) · 4.62 KB
/
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package websocket
import (
"log"
"github.com/gorilla/websocket"
)
/*
* This structure will define a client that connected to the chat service.
* Each client will have a unique id, a websocket connection that will be used to send and receive messages
* The client structure will also have a pointer to the pool structure which will be used for conccurency
*/
type DashboardClient struct {
Id int64
Status string
Conn *websocket.Conn
Pool *Pool
}
type AgentClient struct {
Id string
Status string
Conn *websocket.Conn
Pool *Pool
}
/*
* This structure will define a message that can be sent/received on the websocket
* The type variable will be used to determine if the websocket message is text or binary as it will have different values based on that
* The body will be the payload that the other users it is delivered to should receive
*/
type DashboardMessage struct {
C *DashboardClient
Type int `json:"type"`
Body string `json:"body"`
}
type AgentMessage struct {
C *AgentClient
Type int `json:"type"`
Body string `json:"body"`
}
// // Constants
// const (
// // Time allowed to write a message to the peer.
// writeWait = 10 * time.Second
// // Time allowed to read the next pong message from the peer.
// pongWait = 60 * time.Second
// // Send pings to peer with this period. Must be less than pongWait.
// pingPeriod = (pongWait * 9) / 10
// // Maximum message size allowed from peer.
// maxMessageSize = 512
// )
/*
* This function will wait for a message to be sent by the client and based on the message type different functions from the pool will be called
*/
func (c *DashboardClient) Read() {
//Unregister a client when it disconnects from the server (this function will be called after the infinite loop)
defer func() {
c.Pool.UnregisterDashboard <- c
c.Conn.Close()
}()
// c.Conn.SetReadLimit(maxMessageSize)
// c.Conn.SetReadDeadline(time.Now().Add(pongWait))
// c.Conn.SetPongHandler(func(string) error {
// c.Pool.logger.Debug("Received pong message from dashboard")
// c.Conn.SetReadDeadline(time.Now().Add(pongWait))
// return nil
// })
//Check if a message is received from the client
for {
//read the message from the client (here is where the message type is specified)
messageType, p, err := c.Conn.ReadMessage()
if err != nil {
log.Println(err)
return
}
//Create the message structure based on the message received from the client
message := DashboardMessage{Type: messageType, Body: string(p), C: c}
//Broadcast the message to all the other users (TO DO ... create private conversations and groups)
c.Pool.DashboardBroadcast <- message
}
}
// func (c *DashboardClient) Write() {
// ticker := time.NewTicker(pingPeriod)
// defer func() {
// ticker.Stop()
// c.Conn.Close()
// }()
// for ; ; <-ticker.C {
// c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
// c.Pool.logger.Debug("Sent pong message to dashboard")
// if err := c.Conn.WriteMessage(websocket.PingMessage, []byte("Test")); err != nil {
// c.Pool.logger.Debug(err.Error())
// return
// }
// }
// }
/*
* This function will wait for a message to be sent by the client and based on the message type different functions from the pool will be called
*/
func (c *AgentClient) Read() {
//Unregister a client when it disconnects from the server (this function will be called after the infinite loop)
defer func() {
c.Pool.UnregisterAgent <- c
c.Conn.Close()
}()
// c.Conn.SetReadLimit(maxMessageSize)
// c.Conn.SetReadDeadline(time.Now().Add(pongWait))
// c.Conn.SetPongHandler(func(string) error {
// c.Pool.logger.Debug("Received pong message from agent")
// c.Conn.SetReadDeadline(time.Now().Add(pongWait))
// return nil
// })
//Check if a message is received from the server
for {
//Read the message from the server
messageType, p, err := c.Conn.ReadMessage()
if err != nil {
c.Pool.logger.Error("Error occured when reading message from agent", c.Id, "on the websocket,", err.Error())
return
}
//Create the message structure based on the message received from the client
message := AgentMessage{Type: messageType, Body: string(p), C: c}
//Send the message to the WS message handler
c.Pool.AgentBroadcast <- message
}
}
// func (c *AgentClient) Write() {
// ticker := time.NewTicker(pingPeriod)
// defer func() {
// ticker.Stop()
// c.Conn.Close()
// }()
// for ; ; <-ticker.C {
// c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
// c.Pool.logger.Debug("Sent pong message to agent")
// if err := c.Conn.WriteMessage(websocket.PingMessage, []byte("Test")); err != nil {
// c.Pool.logger.Debug(err.Error())
// return
// }
// }
// }