-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
64 lines (57 loc) · 1.25 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
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
type Client struct {
hub *Hub
conn *websocket.Conn
send chan []Country
}
func (c *Client) writeList() {
defer func() {
c.hub.unregister <- c
c.conn.Close()
}()
for msg := range c.send {
err := c.conn.WriteJSON(msg)
if err != nil {
return
}
}
}
// func (c *Client) pingClient() {
// defer func() {
// c.hub.unregister <- c
// c.conn.Close()
// }()
// var pingPeriod = 30 * time.Second
// var writeWait = 5 * time.Second
// ticker := time.NewTicker(pingPeriod)
// defer ticker.Stop()
// for {
// select {
// case <-ticker.C:
// log.Println("pinging...")
// if err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait)); err != nil {
// log.Println("ping:", err)
// return
// }
// log.Println("finish")
// default:
// }
// }
// }
func chatHandler(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
client := &Client{hub: hub, conn: conn, send: make(chan []Country)}
client.hub.register <- client
client.conn.WriteJSON(CurList.Body.MedalRank.MedalsList) // initial list
client.writeList()
//client.pingClient()
}