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

5 years ago
5 years ago
5 years ago
5 years ago
  1. package ws
  2. import (
  3. "encoding/json"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "gopkg.in/olahol/melody.v1"
  6. "math"
  7. )
  8. type WsCtl struct {
  9. core.WebSocket
  10. node *Node
  11. }
  12. func (ws *WsCtl) HandleConnect(s *melody.Session) {
  13. if ws.node == nil {
  14. ws.node = N
  15. ws.GetMelody().Config.MaxMessageSize = math.MaxInt16
  16. ws.GetMelody().Config.MessageBufferSize = math.MaxInt16
  17. }
  18. ws.node.rwmux.Lock()
  19. defer ws.node.rwmux.Unlock()
  20. roomId := s.Request.URL.Query().Get("activity_id")
  21. if _, ok := ws.node.rooms[roomId]; !ok { // 创建房间
  22. ws.node.rooms[roomId] = NewRoom(roomId)
  23. }
  24. client := NewClient(s, ws.node, roomId)
  25. ws.node.conns[s] = client
  26. ws.node.handleMelodyConnect(client)
  27. }
  28. func (ws *WsCtl) HandleMessage(s *melody.Session, buf []byte) {
  29. msg := new(Message)
  30. err := json.Unmarshal(buf, msg)
  31. if err != nil {
  32. s.Write([]byte(err.Error()))
  33. return
  34. }
  35. client, ok := ws.node.conns[s] // 鉴定某个链接是否登录
  36. if !ok {
  37. WriteJsonWithSession(s, map[string]interface{}{
  38. "msg": "链接无效",
  39. "code": 505,
  40. })
  41. //s.Write([]byte("链接无效"))
  42. return
  43. }
  44. ws.node.handleMelodyMessage(client, msg)
  45. }
  46. func (ws *WsCtl) HandleDisconnect(s *melody.Session) {
  47. if client, ok := ws.node.conns[s]; ok {
  48. ws.node.handleMelodyDisconnect(client)
  49. }
  50. ws.node.rwmux.Lock()
  51. defer ws.node.rwmux.Unlock()
  52. delete(ws.node.conns, s)
  53. }
  54. func (ws *WsCtl) HandleError(s *melody.Session, err error) {
  55. }
  56. func (ws *WsCtl) HandleMessageBinary(s *melody.Session, msg []byte) {
  57. }
  58. func (ws *WsCtl) HandlePong(s *melody.Session) {
  59. }
  60. func (ws *WsCtl) HandleSentMessage(s *melody.Session, msg []byte) {
  61. }
  62. func (ws *WsCtl) HandleSentMessageBinary(*melody.Session, []byte) {
  63. }