You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
package ws
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/ouxuanserver/osmanthuswine/src/core"
|
|
"gopkg.in/olahol/melody.v1"
|
|
"math"
|
|
)
|
|
|
|
type WsCtl struct {
|
|
core.WebSocket
|
|
node *Node
|
|
}
|
|
|
|
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.node.rwmux.Lock()
|
|
defer ws.node.rwmux.Unlock()
|
|
roomId := s.Request.URL.Query().Get("activity_id")
|
|
if _, ok := ws.node.rooms[roomId]; !ok { // 创建房间
|
|
ws.node.rooms[roomId] = NewRoom(roomId)
|
|
}
|
|
client := NewClient(s, ws.node, roomId)
|
|
ws.node.conns[s] = client
|
|
ws.node.handleMelodyConnect(client)
|
|
}
|
|
|
|
func (ws *WsCtl) HandleMessage(s *melody.Session, buf []byte) {
|
|
msg := new(Message)
|
|
err := json.Unmarshal(buf, msg)
|
|
if err != nil {
|
|
s.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
|
|
client, ok := ws.node.conns[s] // 鉴定某个链接是否登录
|
|
if !ok {
|
|
WriteJsonWithSession(s, map[string]interface{}{
|
|
"msg": "链接无效",
|
|
"code": 505,
|
|
})
|
|
//s.Write([]byte("链接无效"))
|
|
return
|
|
}
|
|
|
|
ws.node.handleMelodyMessage(client, msg)
|
|
}
|
|
|
|
func (ws *WsCtl) HandleDisconnect(s *melody.Session) {
|
|
if client, ok := ws.node.conns[s]; ok {
|
|
ws.node.handleMelodyDisconnect(client)
|
|
}
|
|
ws.node.rwmux.Lock()
|
|
defer ws.node.rwmux.Unlock()
|
|
delete(ws.node.conns, s)
|
|
}
|
|
|
|
func (ws *WsCtl) HandleError(s *melody.Session, err error) {
|
|
|
|
}
|
|
|
|
func (ws *WsCtl) HandleMessageBinary(s *melody.Session, msg []byte) {
|
|
|
|
}
|
|
|
|
func (ws *WsCtl) HandlePong(s *melody.Session) {
|
|
|
|
}
|
|
|
|
func (ws *WsCtl) HandleSentMessage(s *melody.Session, msg []byte) {
|
|
|
|
}
|
|
|
|
func (ws *WsCtl) HandleSentMessageBinary(*melody.Session, []byte) {
|
|
|
|
}
|