-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsockets.go
More file actions
44 lines (38 loc) · 801 Bytes
/
websockets.go
File metadata and controls
44 lines (38 loc) · 801 Bytes
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
package main
import (
"fmt"
"log"
"net/http"
"code.google.com/p/go.net/websocket"
)
func websockets_client(conn *websocket.Conn) {
client := new(client)
client.init(conn.Request().RemoteAddr)
defer conn.Close()
defer client.disconnect()
stats_channel <- stat_bump{stat: "connections", val: 1}
if cfg.Verbose {
fmt.Printf("%s connected\n", client.me)
}
for {
var input []byte
err := websocket.Message.Receive(conn, &input)
if err != nil {
return
}
_, err = conn.Write(client.command(input))
if err != nil {
return
}
}
}
func mind_websockets() {
if cfg.Ws == 0 {
return
}
http.Handle("/", websocket.Handler(websockets_client))
err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.Ws), nil)
if err != nil {
log.Fatalf("HTTP Listening Error: %+v", err)
}
}