-
Notifications
You must be signed in to change notification settings - Fork 3
/
ws_connection.go
119 lines (101 loc) · 2.95 KB
/
ws_connection.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"log"
"code.google.com/p/go.net/websocket"
"github.com/garyburd/redigo/redis"
)
// WebSocket Message
//
// Used for JSON conversion.
// action = SUBSCRIBE|UNSUBSCRIBE|PUBLISH
// channel = Redis channel
// data = Message to be sent
type WSMessage struct {
Action string `json:"action"`
Channel string `json:"channel"`
Data string `json:"data"`
}
// WebSocket Connection
//
// Handles incoming and outcoming websocket data by communicating
// with Redis via its PubSub commands.
type WSConnection struct {
socket *websocket.Conn
publish *redis.PubSubConn
subscribe *redis.PubSubConn
}
// Register at WSServer and connect to Redis.
func (wsc *WSConnection) Initialize() {
wss.register <- wsc
wsc.publish = wsc.MakeRedisConnection()
wsc.subscribe = wsc.MakeRedisConnection()
}
// Unregister from WSServer and disconnect from Redis.
func (wsc *WSConnection) Uninitialize() {
log.Println("Uninitialize")
wss.unregister <- wsc
wsc.publish.Close()
wsc.subscribe.Close()
}
// Read from WebSocket
func (wsc *WSConnection) ReadWebSocket() {
for {
var json_data []byte
var message WSMessage
// Receive data from WebSocket
err := websocket.Message.Receive(wsc.socket, &json_data)
if err != nil {
return
}
// Parse JSON data
err = json.Unmarshal(json_data, &message)
if err != nil {
return
}
switch message.Action {
case "SUBSCRIBE":
wsc.subscribe.Subscribe(message.Channel)
case "UNSUBSCRIBE":
wsc.subscribe.Unsubscribe(message.Channel)
case "PUBLISH":
wsc.publish.Conn.Do("PUBLISH", message.Channel, message.Data)
}
}
}
// Proxy incoming data from Redis to WebSocket.
func (wsc *WSConnection) ProxyRedisSubscribe() {
for {
switch reply := wsc.subscribe.Receive().(type) {
case redis.Message:
message := WSMessage {"PUBLISH", reply.Channel, string(reply.Data)}
json_data, err := json.Marshal(message)
if err == nil {
websocket.Message.Send(wsc.socket, string(json_data))
}
case redis.Subscription:
message := WSMessage {"SUBSCRIBE", reply.Channel, ""}
json_data, err := json.Marshal(message)
if err == nil {
websocket.Message.Send(wsc.socket, string(json_data))
}
case error:
return
}
}
}
// Establish a connection to Redis via Redigo PubSubConn
func (wsc *WSConnection) MakeRedisConnection() *redis.PubSubConn {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatal(err)
}
return &redis.PubSubConn{c}
}
func handleWSConnection(socket *websocket.Conn) {
wsc := &WSConnection {socket: socket}
defer wsc.Uninitialize()
wsc.Initialize()
go wsc.ProxyRedisSubscribe()
wsc.ReadWebSocket()
}