-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsockets.go
68 lines (64 loc) · 2.01 KB
/
websockets.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
package main
import (
"code.google.com/p/go.net/websocket"
"code.google.com/p/google-api-go-client/mirror/v1"
"github.com/wearscript/wearscript-go/wearscript"
"fmt"
"strings"
"time"
"sync"
)
var Locks = map[string]*sync.Mutex{} // [user]
var UserSockets = map[string][]chan *[]interface{}{} // [user]
var Managers = map[string]*wearscript.ConnectionManager{}
func CurTime() float64 {
return float64(time.Now().UnixNano()) / 1000000000.
}
func WSHandler(ws *websocket.Conn) {
defer ws.Close()
fmt.Println("Connected with glass")
fmt.Println(ws.Request().URL.Path)
userId, err := userID(ws.Request())
if err != nil || userId == "" {
path := strings.Split(ws.Request().URL.Path, "/")
fmt.Println(path)
if len(path) != 3 {
fmt.Println("Bad path")
return
}
userId, err = getSecretUser("ws", secretHash(path[len(path)-1]))
if err != nil {
fmt.Println(err)
return
}
}
userId = SanitizeUserId(userId)
_, err = mirror.New(authTransport(userId).Client()) // svc
if err != nil {
LogPrintf("ws: mirror")
//WSSend(c, &[]interface{}{[]uint8("error"), []uint8("Unable to create mirror transport")})
return
}
// TODO(brandyn): Add lock here when adding users
if Managers[userId] == nil {
Managers[userId], err = wearscript.ConnectionManagerFactory("server", "0")
if err != nil {
LogPrintf("ws: cm")
return
}
Managers[userId].SubscribeTestHandler()
Managers[userId].Subscribe("gist", func(channel string, dataRaw []byte, data []interface{}) {
GithubGistHandle(Managers[userId], userId, data)
})
Managers[userId].Subscribe("weariverse", func(channel string, dataRaw []byte, data []interface{}) {
WeariverseGistHandle(Managers[userId], userId, data)
})
}
cm := Managers[userId]
conn, err := cm.NewConnection(ws) // con
if err != nil {
fmt.Println("Failed to create conn")
return
}
cm.HandlerLoop(conn)
}