2 Star 2 Fork 0

sdxstar / air

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
context.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
克拉玛干 提交于 2021-09-20 00:02 . 1.修复项目结构
package air
import (
"net"
"gitee.com/sdxstar/air/codec"
"gitee.com/sdxstar/air/logger"
"gitee.com/sdxstar/air/protocol"
)
type Context interface {
Id() string
IsClosed() bool
Close()
Get(key string) interface{}
Set(key string, value interface{})
RemoteAddr() net.Addr
Emit(event string, v interface{}) error
Ack(event string, v interface{}, r interface{}) error
BroadcastToAll(event string, msg []byte)
BroadcastTo(room, event string, msg []byte)
Join(room string)
Leave(room string)
Content() []byte
Packet() protocol.Packet
Bind(v interface{}) error
Reply(v interface{}) error
Conn() Conn
Logger() logger.Logger
}
type context struct {
conn *connection
p protocol.Packet
}
func newContext(conn *connection, p protocol.Packet) Context {
return &context{
conn: conn,
p: p,
}
}
func (c *context) Reply(v interface{}) error {
if v == nil {
rp := c.p.NewReplyPacket(nil)
return c.conn.reply(rp)
}
msg, err := c.conn.srv.opts.Codec.Encode(v)
if err != nil {
return err
}
rp := c.p.NewReplyPacket(msg)
return c.conn.reply(rp)
}
func (c *context) Packet() protocol.Packet {
return c.p
}
func (c *context) Bind(v interface{}) error {
return codec.Json.Unmarshal(c.p.Body(), v)
}
func (c *context) Content() []byte {
return c.p.Body()
}
func (c *context) Id() string {
return c.conn.sid
}
func (c *context) IsClosed() bool {
return c.conn.IsClosed()
}
func (c *context) Close() {
c.conn.Close()
}
func (c *context) Get(key string) interface{} {
return c.conn.Get(key)
}
func (c *context) Set(key string, value interface{}) {
c.conn.Set(key, value)
}
func (c *context) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *context) Emit(event string, v interface{}) error {
return c.conn.Emit(event, v)
}
func (c *context) Ack(event string, v interface{}, r interface{}) error {
return c.conn.Ack(event, v, r)
}
func (c *context) BroadcastToAll(event string, msg []byte) {
c.conn.BroadcastToAll(event, msg)
}
func (c *context) BroadcastTo(room, event string, msg []byte) {
c.conn.BroadcastTo(room, event, msg)
}
func (c *context) Join(room string) {
c.conn.Join(room)
}
func (c *context) Leave(room string) {
c.conn.Leave(room)
}
func (c *context) Conn() Conn {
return c.conn
}
func (c *context) Logger() logger.Logger {
return c.conn.srv.log
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdxstar/air.git
git@gitee.com:sdxstar/air.git
sdxstar
air
air
v0.5.1

搜索帮助

344bd9b3 5694891 D2dac590 5694891