-
Notifications
You must be signed in to change notification settings - Fork 50
/
context.go
68 lines (56 loc) · 1.55 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
package node
import (
"context"
"github.com/dobyte/due/v2/session"
)
type Context struct {
ctx context.Context
Proxy *Proxy
Request *Request
Middleware *Middleware
}
// Context 获取上线文
func (c *Context) Context() context.Context {
return c.ctx
}
// BindGate 绑定网关
func (c *Context) BindGate(uid int64) error {
return c.Proxy.BindGate(c.ctx, uid, c.Request.GID, c.Request.CID)
}
// UnbindGate 解绑网关
func (c *Context) UnbindGate() error {
return c.Proxy.UnbindGate(c.ctx, c.Request.UID)
}
// BindNode 绑定节点
func (c *Context) BindNode() error {
return c.Proxy.BindNode(c.ctx, c.Request.UID)
}
// UnbindNode 解绑节点
func (c *Context) UnbindNode() error {
return c.Proxy.UnbindNode(c.ctx, c.Request.UID)
}
// GetIP 获取来自网关的连接IP地址
func (c *Context) GetIP() (string, error) {
return c.Proxy.GetIP(c.ctx, &GetIPArgs{
GID: c.Request.GID,
Kind: session.Conn,
Target: c.Request.CID,
})
}
// Response 对来自网关或节点的请求进行响应(C/S模型)
// 使用此方法响应的(消息序列号和消息路由号)与请求的(消息序列号和消息路由号)保持一致
func (c *Context) Response(message interface{}) error {
return c.Proxy.Response(c.ctx, c.Request, message)
}
// Disconnect 关闭来自网关的连接
func (c *Context) Disconnect(isForce ...bool) error {
args := &DisconnectArgs{
GID: c.Request.GID,
Kind: session.Conn,
Target: c.Request.CID,
}
if len(isForce) > 0 {
args.IsForce = isForce[0]
}
return c.Proxy.Disconnect(c.ctx, args)
}