/
httpserver.go
140 lines (109 loc) · 2.84 KB
/
httpserver.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
package main
import (
"log"
"net/http"
"strconv"
"strings"
"sync"
"code.google.com/p/go.net/websocket"
)
const listenAddr = "localhost:4000"
func main() {
go broadcastMsg()
http.HandleFunc("/", rootHandler)
http.Handle("/socket", websocket.Handler(socketHandler))
if err := http.ListenAndServe(listenAddr, nil); err != nil {
log.Fatal(err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
log.Println("rootHandler")
rootTemplate.Execute(w, listenAddr)
}
var gid = 0
var read = make(chan Msg, 10)
var conns = ConnMap{make(map[string]*websocket.Conn), sync.RWMutex{}}
func socketHandler(conn *websocket.Conn) {
log.Println("socketHandler")
gid++
owner := "p"+strconv.Itoa(gid)
if conns.Exists(owner) {
conn.Write([]byte("duplicate id\n"))
return
}
conns.Insert(owner, conn)
msg := Msg{"system", []byte(owner + " login!\n")}
read <- msg
log.Println(conn.RemoteAddr(), "connected!")
readMsg(conn, owner)
log.Println(conn.RemoteAddr(), "disconnected!")
conns.Delete(owner)
conn.Close()
msg = Msg{"system", []byte(owner + " logout!\n")}
read <- msg
}
type Msg struct {
owner string
data []byte
}
type ConnMap struct {
conns map[string]*websocket.Conn
mutex sync.RWMutex
}
func (cm *ConnMap) Insert(owner string, conn *websocket.Conn) {
cm.mutex.Lock()
defer cm.mutex.Unlock()
cm.conns[owner] = conn
}
func (cm *ConnMap) Exists(owner string) (bool) {
cm.mutex.RLock()
defer cm.mutex.RUnlock()
_, ok := cm.conns[owner]
return ok
}
func (cm *ConnMap) Delete(owner string) {
cm.mutex.Lock()
defer cm.mutex.Unlock()
delete(cm.conns, owner)
}
func readMsg(conn *websocket.Conn, owner string) {
tmp := make([]byte, 32)
buf := make([]byte, 0)
for {
readlen, err := conn.Read(tmp)
if err != nil {
break
}
buf = append(buf, tmp[0 : readlen]...)
lensep := strings.Index(string(buf), ":")
if lensep < 0 {
continue
}
msglen, err := strconv.Atoi(string(buf[0:lensep]))
if err != nil {
log.Println("error: ", err)
break
}
if len(buf) < msglen + lensep + 1 {
continue
}
msg := Msg{owner, make([]byte, msglen)}
copy(msg.data, buf[lensep + 1 : msglen + lensep + 1])
read <- msg
buf = buf[lensep + msglen + 1: ]
}
}
func broadcastMsg() {
for {
msg := <-read
buf := make([]byte, 0, len(msg.data) + len(msg.owner) + 2)
buf = append(buf, msg.owner...)
buf = append(buf, ": "...)
buf = append(buf, msg.data...)
conns.mutex.RLock()
for _,conn := range conns.conns {
conn.Write(buf)
}
conns.mutex.RUnlock()
}
}