From 879713a43393dc85259af60f1b9fc308cae4227f Mon Sep 17 00:00:00 2001 From: Tooooommy Date: Mon, 12 Oct 2020 16:43:43 +0800 Subject: [PATCH] fix:bug --- hdws/ws/client.go | 1 + hdws/ws/login.go | 80 ++++++++++++++----------------------------------------- hdws/ws/msg.go | 2 ++ hdws/ws/node.go | 3 +-- hdws/ws/ws.go | 8 +++--- 5 files changed, 27 insertions(+), 67 deletions(-) diff --git a/hdws/ws/client.go b/hdws/ws/client.go index ec8b09b..e092440 100644 --- a/hdws/ws/client.go +++ b/hdws/ws/client.go @@ -35,6 +35,7 @@ func (c *Client) Register() error { } func (c *Client) Send(msg *Message) { + msg.RoomId = c.RoomId c.node.Send(msg) } diff --git a/hdws/ws/login.go b/hdws/ws/login.go index e3d7bf2..eed8d3a 100644 --- a/hdws/ws/login.go +++ b/hdws/ws/login.go @@ -3,6 +3,7 @@ package ws import ( "fmt" "hudongzhuanjia/models" + "hudongzhuanjia/utils" ) func init() { @@ -14,74 +15,33 @@ func LoginMid(c *Client, msg *Message) { var token string var ok bool if token, ok = msg.Data["token"].(string); !ok || token == "" { - _ = c.WriteJson(map[string]interface{}{ - "msg": "token不能为空", - "code": 506, - }) - return - } - - _type, id, err := models.ParseToken(token) - if err != nil { - c.WriteJson(map[string]interface{}{ - "msg": "token失效", - "code": 507, - }) - return + c.Online = true + c.AccountId = utils.RandomInt(6) + c.AccountType = "guest" + c.Id = fmt.Sprintf("%s:%d", c.AccountType, c.AccountId) + } else { + _type, id, err := models.ParseToken(token) + if err != nil { + c.WriteJson(map[string]interface{}{ + "msg": "token失效", + "code": 507, + }) + return + } + c.Online = true + c.AccountId = id + c.AccountType = _type + c.Id = fmt.Sprintf("%s:%d", c.AccountType, c.AccountId) } - c.Online = true - c.AccountId = id - c.AccountType = _type - c.Id = fmt.Sprintf("%s:%d", c.AccountType, c.AccountId) - //claims, err := ParseAccessToken(msg.Data["token"].(string)) - //if err != nil { - // _ = c.WriteJson(map[string]interface{}{ - // "msg": "token解析出错, " + err.Error(), - // "code": 507, - // }) - // return - //} - //c.Online = true - //c.Id = fmt.Sprintf("%s:%d", claims.AccountType, claims.AccountId) - //c.AccountId = claims.AccountId - //c.AccountType = claims.AccountType - //c.Pid = claims.CustomerPid - if err = c.Register(); err != nil { + if err := c.Register(); err != nil { _ = c.WriteJson(map[string]interface{}{ "msg": err.Error(), "code": 404, }) return } - // - //if claims.AccountType == IDCustomer { - // if c.Pid == 0 { // 主账号发给子账号 - // m := &Message{ - // Type: TypeNotice, - // Dest: 0, - // Tag: IDCustomer, - // RoomId: c.RoomId, - // From: c.Id, - // Data: map[string]interface{}{ - // "content": "主账号上线", - // }, - // } - // c.Send(m) - // } else { // 子账号发给主账号 - // m := &Message{ - // Type: TypeNotice, - // Dest: claims.CustomerPid, - // Tag: IDCustomer, - // RoomId: c.RoomId, - // From: c.Id, - // Data: map[string]interface{}{ - // "content": "子账号上线", - // }, - // } - // c.Send(m) - // } - //} _ = c.WriteJson(map[string]interface{}{ + "id": c.Id, "msg": "登录成功", "code": 200, }) diff --git a/hdws/ws/msg.go b/hdws/ws/msg.go index 85ecd2e..5664150 100644 --- a/hdws/ws/msg.go +++ b/hdws/ws/msg.go @@ -6,5 +6,7 @@ func init() { // 简单明了 func msgFunc(c *Client, msg *Message) { + msg.RoomId = c.RoomId + msg.From = c.Id c.Send(msg) } diff --git a/hdws/ws/node.go b/hdws/ws/node.go index 5291e2e..00affda 100644 --- a/hdws/ws/node.go +++ b/hdws/ws/node.go @@ -139,9 +139,8 @@ func (t *Node) Send(msg *Message) { t.BroadcastTag(msg) } else if msg.Dest != 0 && msg.Tag != "" { t.BroadcastDest(msg) - } else { - log.Printf("[websocket] [send] msg=>%v", msg) } + log.Printf("[websocket] [send] msg=>%+v", msg) } // 发送给所有的 diff --git a/hdws/ws/ws.go b/hdws/ws/ws.go index 934d0d6..7f0acde 100644 --- a/hdws/ws/ws.go +++ b/hdws/ws/ws.go @@ -15,8 +15,8 @@ type WsCtl struct { func (ws *WsCtl) HandleConnect(s *melody.Session) { if ws.node == nil { ws.node = N - ws.GetMelody().Config.MaxMessageSize = math.MaxInt16 - ws.GetMelody().Config.MessageBufferSize = math.MaxInt16 + ws.GetMelody().Config.MaxMessageSize = math.MaxInt16 * 10 + ws.GetMelody().Config.MessageBufferSize = math.MaxInt8 } ws.node.rwmux.Lock() defer ws.node.rwmux.Unlock() @@ -28,9 +28,7 @@ func (ws *WsCtl) HandleConnect(s *melody.Session) { client := NewClient(s, ws.node, roomId) token := query.Get("token") - if token != "" { - LoginMid(client, &Message{Data: map[string]interface{}{"token": token}}) // 登录设置 - } + LoginMid(client, &Message{Data: map[string]interface{}{"token": token}}) // 登录设置 ws.node.conns[s] = client ws.node.handleMelodyConnect(client) }