-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
46 lines (38 loc) · 855 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
41
42
43
44
45
46
package main
import (
"fmt"
"log"
"github.com/gorilla/websocket"
)
type client struct {
socket *websocket.Conn
send chan string
room *room
}
type hxJson struct {
ChatMessage string `json:"chatMessage"`
Headers any `json:"Headers"`
}
func (c *client) read() {
defer c.socket.Close()
for {
hxJSON := &hxJson{ChatMessage: "Hello"}
err := c.socket.ReadJSON(hxJSON)
if err != nil {
log.Println(err.Error())
log.Println("Can't read the message from socket")
}
c.room.forward <- hxJSON.ChatMessage
}
}
func (c *client) write() {
defer c.socket.Close()
for msg := range c.send {
log.Println("Sending message to client")
partial := fmt.Sprintf("<p id='chat-box' hx-swap-oob=beforeend>%s <br></p> ", msg)
err := c.socket.WriteMessage(websocket.TextMessage, []byte(partial))
if err != nil {
return
}
}
}