|
|
@ -13,7 +13,6 @@
|
|
|
|
package ws
|
|
|
|
package ws
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jageros/hawox/contextx"
|
|
|
|
"github.com/jageros/hawox/contextx"
|
|
|
@ -53,7 +52,7 @@ func Init(ctx contextx.Context, r *gin.RouterGroup, relativePath string) {
|
|
|
|
callTimeout: time.Second * 5,
|
|
|
|
callTimeout: time.Second * 5,
|
|
|
|
online: map[string]struct{}{},
|
|
|
|
online: map[string]struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss.m.HandleMessage(ss.handleMessage)
|
|
|
|
ss.m.HandleMessageBinary(ss.handleMessage)
|
|
|
|
ss.m.HandleConnect(ss.onConnect)
|
|
|
|
ss.m.HandleConnect(ss.onConnect)
|
|
|
|
ss.m.HandleDisconnect(ss.onDisconnect)
|
|
|
|
ss.m.HandleDisconnect(ss.onDisconnect)
|
|
|
|
r.GET(relativePath, ss.handler)
|
|
|
|
r.GET(relativePath, ss.handler)
|
|
|
@ -106,8 +105,9 @@ func (s *service) updateOnline() {
|
|
|
|
MsgID: 2,
|
|
|
|
MsgID: 2,
|
|
|
|
Msg: msg,
|
|
|
|
Msg: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bty, err := json.Marshal(resp)
|
|
|
|
bty, err := types.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logx.Errorf("编码错误:%v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = s.m.BroadcastBinary(bty)
|
|
|
|
err = s.m.BroadcastBinary(bty)
|
|
|
@ -120,7 +120,6 @@ func (s *service) updateOnline() {
|
|
|
|
func (s *service) handleMessage(session *melody.Session, bytes []byte) {
|
|
|
|
func (s *service) handleMessage(session *melody.Session, bytes []byte) {
|
|
|
|
start := time.Now()
|
|
|
|
start := time.Now()
|
|
|
|
uid, exist := session.Get("uid")
|
|
|
|
uid, exist := session.Get("uid")
|
|
|
|
logx.Infof("on msg uid=%s", uid)
|
|
|
|
|
|
|
|
if !exist {
|
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -131,16 +130,23 @@ func (s *service) handleMessage(session *melody.Session, bytes []byte) {
|
|
|
|
defer mux.Unlock()
|
|
|
|
defer mux.Unlock()
|
|
|
|
seq += 1
|
|
|
|
seq += 1
|
|
|
|
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("[%d]%s(%s): %s\n", seq, name, time.Now().Format("15:04:05"), string(bytes))
|
|
|
|
resp, err := types.Unmarshal(bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
var resp = &types.Msg{
|
|
|
|
logx.Errorf("解码错误:%v", err)
|
|
|
|
MsgID: 1,
|
|
|
|
return
|
|
|
|
Msg: msg,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bty, err := json.Marshal(resp)
|
|
|
|
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("[%d]%s(%s): %s\n", seq, name, time.Now().Format("15:04:05"), resp.Msg)
|
|
|
|
|
|
|
|
resp.Msg = msg
|
|
|
|
|
|
|
|
resp.MsgID = 1
|
|
|
|
|
|
|
|
resp.Seq = seq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bty, err := types.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
logx.Errorf("编码错误:%v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = s.m.BroadcastBinary(bty)
|
|
|
|
err = s.m.BroadcastBinary(bty)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
logx.Error(err)
|
|
|
|