forked from wingyplus/websocket-echoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (62 loc) · 1.53 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
package main
import (
"code.google.com/p/go.net/websocket"
"fmt"
"io"
"net/http"
)
type Server struct {
Clients map[*websocket.Conn]string
Room map[string][]*websocket.Conn
}
func (s *Server) Add(roomName string, c *websocket.Conn) {
s.Room[roomName] = append(s.Room[roomName], c)
s.Clients[c] = roomName
}
func (s *Server) Remove(ws *websocket.Conn) {
roomName := s.Clients[ws]
delete(s.Clients, ws)
clients := s.Room[roomName]
for i, c := range clients {
if ws == c {
s.Room[roomName] = append(s.Room[roomName][:i], s.Room[roomName][i+1:]...)
}
}
if len(s.Room[roomName]) == 0 {
delete(s.Room, roomName)
}
}
var server = &Server{
Clients: make(map[*websocket.Conn]string),
Room: make(map[string][]*websocket.Conn),
}
func EchoServer(ws *websocket.Conn) {
var req map[string]string
defer server.Remove(ws)
for websocket.JSON.Receive(ws, &req) != io.EOF {
fmt.Println(ws.Request().RemoteAddr)
switch req["event"] {
case "ADD":
roomName := req["roomName"]
server.Add(roomName, ws)
fmt.Println(server.Room)
case "ECHO":
for _, client := range server.Room[req["roomName"]] {
websocket.Message.Send(client, req["message"])
}
case "CLOSE":
fmt.Println(req["message"])
}
}
fmt.Println("connection ", ws)
fmt.Println(server.Clients)
fmt.Println(server.Room)
}
func main() {
http.Handle("/", http.FileServer(http.Dir("www")))
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}