/
server.go
67 lines (55 loc) · 1.39 KB
/
server.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
package main
import (
"fmt"
"net/http"
"html/template"
"code.google.com/p/go.net/websocket"
)
func main() {
handle("/static/", http.FileServer(http.Dir("./static")))
handle("/draft/", http.HandlerFunc(getDraftPage))
handle("/ws/", websocket.Handler(wsJoin))
err := http.ListenAndServe(":80", nil)
if err != nil {
panic(err.Error())
}
}
func handle(prefix string, h http.Handler) {
http.Handle(prefix, http.StripPrefix(prefix, h))
}
type WsConnection struct {
Receive chan MessageIn
Send chan MessageOut
}
var draft = new(DraftTable)
func wsJoin(ws *websocket.Conn) {
//try to get the draft
//if it doesn't exist, close
conn := WsConnection{ make(chan MessageIn), make(chan MessageOut) }
//send to draft, handle error
//test code
draft.Connections = []WsConnection{ conn }
go draft.RunDraft()
go func() {
defer close(conn.Send)
for msgOut := range conn.Send {
websocket.JSON.Send(ws, msgOut)
//handle error
}
}()
defer close(conn.Receive)
var msgIn MessageIn
for {
websocket.JSON.Receive(ws, &msgIn)
//handle error
conn.Receive <- msgIn
}
}
var draftTmpl, _ = template.ParseFiles("draft.tmpl")
func getDraftPage(wr http.ResponseWriter, req *http.Request) {
err := draftTmpl.Execute(wr, nil)
if err != nil {
fmt.Println(err.Error())
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
}