-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.go
61 lines (54 loc) · 1.44 KB
/
messages.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
package main
import (
"encoding/json"
"fmt"
"log"
)
const ARENA_LOGIN = 101
const ARENA_HEARTBEAT = 100
const ARENA_SUBSCRIBE = 108
type ArenaMessage struct {
Bm struct {
Payload map[string]interface{} `json:"payload,omitempty"`
SessionID string `json:"sessionId,omitempty"`
User int32 `json:"user,omitempty"`
Pid int `json:"pid"`
Csq int `json:"csq"`
} `json:"bm"`
}
func (msg *ArenaMessage) mkCmd(command int, params map[string]interface{}) []byte {
csq++
msg.Bm.Pid = command
msg.Bm.Csq = csq
msg.Bm.Payload = params
msg.Bm.SessionID = sid
msg.Bm.User = uid
data, err := json.Marshal(msg)
if err != nil {
log.Panic("Undefined behaivour")
}
header := []byte(fmt.Sprintf("%d:", len(data)))
data = append(header, data...)
log.Println(string(data))
return data
}
func processMessage(rawMessage []byte) [][]byte {
var reply [][]byte
var msg ArenaMessage
err := json.Unmarshal(rawMessage, &msg)
if err == nil {
if msg.Bm.Pid == 101 {
userInfo := msg.Bm.Payload["user"].(map[string]interface{})
sid = userInfo["sid"].(string)
uid = msg.Bm.User
for _, symbol := range symbols {
reply = append(reply, (&ArenaMessage{}).mkCmd(ARENA_SUBSCRIBE, map[string]interface{}{"@class": "p.Subscription",
"exchange": "BVB", "symbol": symbol}))
}
return reply
}
} else {
log.Panic("Unable to unmarshall", err)
}
return nil
}