-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
62 lines (53 loc) · 1.17 KB
/
response.go
File metadata and controls
62 lines (53 loc) · 1.17 KB
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
package gosocket
type ArgsRequest struct {
Id string `json:"id"`
Args interface{} `json:"args"`
}
type ArgsResponse struct {
Id string `json:"id"`
Args response `json:"args"`
}
type response struct {
Result bool `json:"result"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
type Response struct {
clientId string // client connection id
client ClientFace
event string
id string // client transparent id
response
}
func NewResponse(client ClientFace, event, clientId, id string) (r *Response) {
r = new(Response)
r.client = client
r.event = event
r.clientId = clientId
r.id = id
return
}
func (r *Response) Set(result bool, message string, data interface{}) {
r.Result = result
r.Message = message
r.Data = data
return
}
func (r *Response) Success(data interface{}) {
r.Set(true, "ok", data)
r.Emit()
}
func (r *Response) Fail(message string) {
r.Set(false, message, nil)
r.Emit()
}
func (r *Response) Emit() {
if r.clientId == "" {
r.client.Emit(r.event, r.response, r.id)
} else {
var args ArgsResponse
args.Id = r.clientId
args.Args = r.response
r.client.Emit(r.event, args, r.id)
}
}