-
Notifications
You must be signed in to change notification settings - Fork 41
/
context.go
73 lines (63 loc) · 1.4 KB
/
context.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
package realtime
import (
"encoding/json"
)
type Context struct {
Client *Client
FullPath string
params map[string]string
properties map[string]interface{}
}
type Error struct {
Code int `json:"code"`
Description string `json:"description"`
}
func NewError(code int, description string) *Error {
return &Error{
Code: code,
Description: description,
}
}
func (context *Context) Get(key string) (value interface{}, exists bool) {
if val, ok := context.properties[key]; ok {
return val, ok
}
return nil, false
}
func (context *Context) Set(key string, value interface{}) {
context.properties[key] = value
}
func (context *Context) SendError(error *Error) error {
data, err := json.Marshal(error)
if err != nil {
return err
}
message := Message{
Type: MessageTypeChannelMessage,
Channel: context.FullPath,
Payload: data,
}
data, err = json.Marshal(message)
if err != nil {
return err
}
return context.Client.Send(data)
}
func (context *Context) Send(payload []byte) error {
message := Message{
Type: MessageTypeChannelMessage,
Channel: context.FullPath,
Payload: payload,
}
data, err := json.Marshal(message)
if err != nil {
return err
}
return context.Client.Send(data)
}
func (context *Context) SetParams(params map[string]string) {
context.params = params
}
func (context *Context) Param(key string) string {
return context.params[key]
}