Browse Source

fix:bug

dev
Tooooommy 4 years ago
parent
commit
879713a433
  1. 1
      hdws/ws/client.go
  2. 58
      hdws/ws/login.go
  3. 2
      hdws/ws/msg.go
  4. 3
      hdws/ws/node.go
  5. 6
      hdws/ws/ws.go

1
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)
}

58
hdws/ws/login.go

@ -3,6 +3,7 @@ package ws
import (
"fmt"
"hudongzhuanjia/models"
"hudongzhuanjia/utils"
)
func init() {
@ -14,13 +15,11 @@ 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
}
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{}{
@ -33,55 +32,16 @@ func LoginMid(c *Client, msg *Message) {
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,
})

2
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)
}

3
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)
}
// 发送给所有的

6
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}}) // 登录设置
}
ws.node.conns[s] = client
ws.node.handleMelodyConnect(client)
}

Loading…
Cancel
Save