-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 1.34 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"time"
"golang.org/x/net/websocket"
)
type Server struct {
conns map[*websocket.Conn]bool
}
func NewServer() *Server {
return &Server{
conns: make(map[*websocket.Conn]bool),
}
}
func (s *Server) handleWSSubscribe(ws *websocket.Conn) {
log.Println("new connection from client to [subscribe]:", ws.RemoteAddr())
for {
payload := fmt.Sprintf("subscribe data ==> %d\n", time.Now().UnixNano())
ws.Write([]byte(payload))
time.Sleep(time.Second * 2)
}
}
func (s *Server) handleWS(ws *websocket.Conn) {
log.Println("new connection from client:", ws.RemoteAddr())
s.conns[ws] = true
s.readLoop(ws)
}
func (s *Server) readLoop(ws *websocket.Conn) {
buf := make([]byte, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
if err == io.EOF {
break
}
log.Fatalln("read error:", err)
continue
}
msg := buf[:n]
s.broadcast(msg)
}
}
func (s *Server) broadcast(b []byte) {
for ws := range s.conns {
go func(ws *websocket.Conn) {
if _, err := ws.Write(b); err != nil {
log.Fatalln("write error:", err)
}
}(ws)
}
}
func main() {
server := NewServer()
http.Handle("/ws", websocket.Handler(server.handleWS))
http.Handle("/sub", websocket.Handler(server.handleWSSubscribe))
log.Println("WebSocket server run on 3000")
http.ListenAndServe(":3000", nil)
}