-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
129 lines (115 loc) · 3.05 KB
/
ws.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
120
121
122
123
124
125
126
127
128
129
package main
import (
"code.google.com/p/go.net/websocket"
"fmt"
"log"
"strconv"
)
const (
// Types of incoming messages
MESSAGE_TYPE_GET = 1
MESSAGE_TYPE_POST = 2
MESSAGE_TYPE_PUT = 3
MESSAGE_TYPE_DELETE = 4
MESSAGE_TYPE_SUBSCRIBE = 11
MESSAGE_TYPE_UNSUBSCRIBE = 12
// Response codees, mapping to HTTP codes
RESPONSE_TYPE_SUCCESS = 200
RESPONSE_TYPE_NOT_FOUND = 404
RESPONSE_TYPE_ERROR = 500
)
var (
messageTypes = map[int]string{
MESSAGE_TYPE_GET: "get",
MESSAGE_TYPE_POST: "post",
MESSAGE_TYPE_PUT: "put",
MESSAGE_TYPE_DELETE: "delete",
MESSAGE_TYPE_SUBSCRIBE: "subscribe",
MESSAGE_TYPE_UNSUBSCRIBE: "unsubscribe",
}
routes = map[string]func(*wsConnection, *wsMessage) wsResponse{
"get:user": getUserHandler,
"post:user": postUserHandler,
"subscribe:user": subscribeUserHandler,
}
)
type wsConnection struct {
ws *websocket.Conn
wsChan chan string
id string
}
// incoming websocket message
type wsMessage [4]string
// outgoing websocket message
type wsResponse [3]string
func wsHandler(ws *websocket.Conn) {
// create a wsConnection instance
wsConn := &wsConnection{
ws: ws,
wsChan: make(chan string),
id: randomId(),
}
// receive a message and respond to it in a goroutine
for {
var message wsMessage
err := websocket.JSON.Receive(wsConn.ws, &message)
if err != nil {
log.Printf("handleMessages Receive error: %v", err)
break
}
go respondToMessage(wsConn, &message)
}
}
func respondToMessage(wsConn *wsConnection, message *wsMessage) {
/* Respond to an incoming message.
Right now, simulates work by sleeping for a few seconds
*/
log.Printf("%v received: %v", wsConn.id, *message)
response := parseAndProcessMessage(wsConn, message)
log.Printf("%v responded: %v", wsConn.id, response)
err := websocket.JSON.Send(wsConn.ws, response)
if err != nil {
log.Printf("handleMessages Send error: %v", err)
return
}
}
func parseAndProcessMessage(wsConn *wsConnection, message *wsMessage) wsResponse {
/*
*/
messageType := messageTypes[message.getType()]
resource := message.getResource()
route := fmt.Sprintf("%s:%s", messageType, resource)
return routes[route](wsConn, message)
}
func errorResponse(wsConn *wsConnection, message *wsMessage, errorMessage string) wsResponse {
return wsResponse{string(RESPONSE_TYPE_ERROR), message.getId(), errorMessage}
}
func successResponse(
wsConn *wsConnection,
message *wsMessage,
response interface{}) wsResponse {
return wsResponse{
strconv.Itoa(RESPONSE_TYPE_SUCCESS),
message.getId(),
fmt.Sprintf("%v", response),
}
}
///
/// methods that operate on a *wsMessage
///
func (message *wsMessage) getId() string {
return (*message)[1]
}
func (message *wsMessage) getType() int {
messageType, _ := strconv.Atoi((*message)[0])
return messageType
}
func (message *wsMessage) getResource() string {
return (*message)[2]
}
func (message *wsMessage) getParams() string {
return (*message)[3]
}
func (message *wsMessage) getParamBytes() []byte {
return []byte(message.getParams())
}