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.

76 lines
1.6 KiB

5 years ago
  1. package connect
  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. s.Write([]byte("链接无效"))
  38. return
  39. }
  40. ws.node.handleMelodyMessage(client, msg)
  41. }
  42. func (ws *WsCtl) HandleDisconnect(s *melody.Session) {
  43. if client, ok := ws.node.conns[s]; ok {
  44. ws.node.handleMelodyDisconnect(client)
  45. }
  46. ws.node.rwmux.Lock()
  47. defer ws.node.rwmux.Unlock()
  48. delete(ws.node.conns, s)
  49. }
  50. func (ws *WsCtl) HandleError(s *melody.Session, err error) {
  51. }
  52. func (ws *WsCtl) HandleMessageBinary(s *melody.Session, msg []byte) {
  53. }
  54. func (ws *WsCtl) HandlePong(s *melody.Session) {
  55. }
  56. func (ws *WsCtl) HandleSentMessage(s *melody.Session, msg []byte) {
  57. }
  58. func (ws *WsCtl) HandleSentMessageBinary(*melody.Session, []byte) {
  59. }