-
Notifications
You must be signed in to change notification settings - Fork 55
/
gameServer.go
47 lines (39 loc) · 1.07 KB
/
gameServer.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
package main
import (
"log"
"net/http"
"code.google.com/p/go.net/websocket"
"github.com/bitly/go-simplejson"
)
func wsHandler(ws *websocket.Conn) {
var err error
var this player // link ws with player
this.ws = ws
// need loop to keep socket connect
for {
var reply string
if err = websocket.Message.Receive(ws, &reply); err != nil {
log.Printf("connect closed!")
break
}
js, err := simplejson.NewJson([]byte(reply))
if err != nil {
// TODO: Send error json back to client
log.Printf("parse json error:", err);
continue
}
commandDispatcher(&this, js)
}
}
func main() {
log.Print("initing database ...");
initDB()
log.Print("initing game script data ...");
initGameData()
log.Print("starting socket server ...");
//TODO read port from config
http.Handle("/", websocket.Handler(wsHandler))
if err := http.ListenAndServe(":1234", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}