互动
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.

84 lines
1.8 KiB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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 * 10
  16. ws.GetMelody().Config.MessageBufferSize = math.MaxInt8
  17. }
  18. ws.node.rwmux.Lock()
  19. defer ws.node.rwmux.Unlock()
  20. query := s.Request.URL.Query()
  21. roomId := query.Get("activity_id")
  22. if _, ok := ws.node.rooms[roomId]; !ok { // 创建房间
  23. ws.node.rooms[roomId] = NewRoom(roomId)
  24. }
  25. client := NewClient(s, ws.node, roomId)
  26. token := query.Get("token")
  27. LoginMid(client, &Message{Data: map[string]interface{}{"token": token}}) // 登录设置
  28. ws.node.conns[s] = client
  29. ws.node.handleMelodyConnect(client)
  30. }
  31. func (ws *WsCtl) HandleMessage(s *melody.Session, buf []byte) {
  32. msg := new(Message)
  33. err := json.Unmarshal(buf, msg)
  34. if err != nil {
  35. s.Write([]byte(err.Error()))
  36. return
  37. }
  38. client, ok := ws.node.conns[s] // 鉴定某个链接是否登录
  39. if !ok {
  40. WriteJsonWithSession(s, map[string]interface{}{
  41. "msg": "链接无效",
  42. "code": 505,
  43. })
  44. //s.Write([]byte("链接无效"))
  45. return
  46. }
  47. ws.node.handleMelodyMessage(client, msg)
  48. }
  49. func (ws *WsCtl) HandleDisconnect(s *melody.Session) {
  50. if client, ok := ws.node.conns[s]; ok {
  51. ws.node.handleMelodyDisconnect(client)
  52. }
  53. ws.node.rwmux.Lock()
  54. defer ws.node.rwmux.Unlock()
  55. delete(ws.node.conns, s)
  56. }
  57. func (ws *WsCtl) HandleError(s *melody.Session, err error) {
  58. }
  59. func (ws *WsCtl) HandleMessageBinary(s *melody.Session, msg []byte) {
  60. }
  61. func (ws *WsCtl) HandlePong(s *melody.Session) {
  62. }
  63. func (ws *WsCtl) HandleSentMessage(s *melody.Session, msg []byte) {
  64. }
  65. func (ws *WsCtl) HandleSentMessageBinary(*melody.Session, []byte) {
  66. }