/
wdeserver.go
60 lines (52 loc) · 1.1 KB
/
wdeserver.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
// Copyright 2013 errnoh. All rights reserved.
// Use of this source code is governed by a BSD-style (2-Clause)
// license that can be found in the LICENSE file.
package canvas
import (
"code.google.com/p/go.net/websocket"
"github.com/skelterjohn/go.wde"
"net/http"
)
const listenAddr = "localhost:12500"
var (
mainwindow = new(Window)
tempvars = new(templateVars)
tick = make(chan struct{})
)
func init() {
wde.BackendNewWindow = NewWindow
ch := make(chan struct{}, 1)
wde.BackendRun = func() {
<-ch
}
wde.BackendStop = func() {
ch <- struct{}{}
}
}
func socketHandler(ws *websocket.Conn) {
mainwindow.ws = ws
go listen(ws)
tick <- struct{}{}
<-mainwindow.end
}
func listen(ws *websocket.Conn) {
// NOTE: Could use websocket.JSON.Receive
var buf = make([]byte, 512)
for {
n, err := ws.Read(buf)
if err != nil {
println(err.Error())
ws.Close()
return
}
if string(buf[:n]) == "ok" {
tick <- struct{}{}
continue
}
forwardEvent(buf[:n])
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
tempvars.Address = r.Host
rootTemplate.Execute(w, tempvars)
}