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

88 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
5 years ago
4 years ago
5 years ago
  1. package ws
  2. import (
  3. "encoding/json"
  4. "git.ouxuan.net/tommy/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. var res = map[string]interface{}{}
  27. for k, v := range query {
  28. res[k] = v[0]
  29. }
  30. LoginMid(client, &Message{Data: res}) // 登录设置
  31. ws.node.conns[s] = client
  32. ws.node.handleMelodyConnect(client)
  33. }
  34. func (ws *WsCtl) HandleMessage(s *melody.Session, buf []byte) {
  35. msg := new(Message)
  36. err := json.Unmarshal(buf, msg)
  37. if err != nil {
  38. s.Write([]byte(err.Error()))
  39. return
  40. }
  41. client, ok := ws.node.conns[s] // 鉴定某个链接是否登录
  42. if !ok {
  43. WriteJsonWithSession(s, map[string]interface{}{
  44. "msg": "链接无效",
  45. "code": 505,
  46. })
  47. //s.Write([]byte("链接无效"))
  48. return
  49. }
  50. ws.node.handleMelodyMessage(client, msg)
  51. }
  52. func (ws *WsCtl) HandleDisconnect(s *melody.Session) {
  53. if client, ok := ws.node.conns[s]; ok {
  54. ws.node.handleMelodyDisconnect(client)
  55. }
  56. ws.node.rwmux.Lock()
  57. defer ws.node.rwmux.Unlock()
  58. delete(ws.node.conns, s)
  59. }
  60. func (ws *WsCtl) HandleError(s *melody.Session, err error) {
  61. }
  62. func (ws *WsCtl) HandleMessageBinary(s *melody.Session, msg []byte) {
  63. }
  64. func (ws *WsCtl) HandlePong(s *melody.Session) {
  65. }
  66. func (ws *WsCtl) HandleSentMessage(s *melody.Session, msg []byte) {
  67. }
  68. func (ws *WsCtl) HandleSentMessageBinary(*melody.Session, []byte) {
  69. }