NetworkWebSocket.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default class NetworkWebSocket {
  2. // // ws://192.168.1.208:3000/pve/room
  3. // url: string = "ws://192.168.1.180:3000/boss/room?uuid=10086&deskId=1"
  4. // // url: string = "ws://192.168.1.208:3000/boss/room?uuid="
  5. // ws: { [key: string]: WebSocket } = {}
  6. // tag: number = 0 // 协议tag
  7. // cbList: { [tag: string]: Function } = {}
  8. // log: boolean = false
  9. // enterRoom(uuid: string) {
  10. // let key = "boss"
  11. // if (this.ws[key] && this.ws[key].readyState === WebSocket.OPEN) {
  12. // return
  13. // }
  14. // this.url = `ws://192.168.1.200:3000/boss/room?uuid=${uuid}&deskId=1`
  15. // if (this.log) {
  16. // console.warn("C->S WS:", this.url)
  17. // }
  18. // this.ws[key] = new WebSocket(this.url)
  19. // this.ws[key].onopen = (evt) => {
  20. // console.log("Connection open...")
  21. // }
  22. // this.ws[key].onclose = (evt) => {
  23. // console.log("Connection close...")
  24. // }
  25. // this.ws[key].onmessage = (evt) => {
  26. // if (this.log) {
  27. // console.warn("S->C WS:", evt.data)
  28. // }
  29. // if (evt.data == null) {
  30. // return
  31. // }
  32. // let result: SevBack = JSON.parse(evt.data)
  33. // if (result == null) {
  34. // console.error("回调数据异常:", evt.data)
  35. // return
  36. // }
  37. // if (result.ws == null) {
  38. // return
  39. // }
  40. // this.onWsRoom(result, key)
  41. // }
  42. // this.ws[key].onerror = (evt) => {
  43. // console.log("Connection error...", evt)
  44. // }
  45. // }
  46. // preTag: string = ""
  47. // send(key: string, params: CS_WS, cb?: (data: SevBack) => void) {
  48. // if (this.preTag.length == 0) {
  49. // this.preTag = FormulaCom.uuid(4, 2)
  50. // }
  51. // if (this.ws[key] == null) {
  52. // console.error("暂未加入房间", key)
  53. // return
  54. // }
  55. // if (this.ws[key].readyState != WebSocket.OPEN) {
  56. // console.error("房间连接异常:", key, this.ws[key].readyState)
  57. // console.error("状态枚举-关闭:", WebSocket.CLOSED)
  58. // console.error("状态枚举-关闭中", WebSocket.CLOSING)
  59. // console.error("状态枚举-连接中", WebSocket.CONNECTING)
  60. // return
  61. // }
  62. // this.tag++
  63. // if (this.tag > 127) {
  64. // this.tag = 1
  65. // }
  66. // if (cb) {
  67. // this.cbList[this.tag] = cb
  68. // }
  69. // params = {
  70. // tag: this.preTag + "_" + this.tag.toString(),
  71. // ...params,
  72. // }
  73. // if (this.log) {
  74. // console.warn("C->S WS:", JSON.stringify(params))
  75. // }
  76. // this.ws[key].send(JSON.stringify(params))
  77. // }
  78. // onWsRoom(result: SevBack, key: string) {
  79. // // if (result.ws == null) {
  80. // // return
  81. // // }
  82. // // console.log("接受数据:", result)
  83. // // 刷新后端数据列表
  84. // let sevback = SevDataMng.sevBackUpdate(GameDataCenter.sevBack, result)
  85. // // 这里去检测通用类数据的前后对比
  86. // GameDataCenter.sevBack = sevback
  87. // // 业务逻辑自定义参数处理
  88. // GameDataCenter._tModel.forEach(element => {
  89. // element.doSevback(result)
  90. // })
  91. // // 业务逻辑事件触发
  92. // GameDataCenter._tModel.forEach(element => {
  93. // element.doEvent(result)
  94. // })
  95. // // 执行协议回调
  96. // if (result.ws.tag && this.cbList[result.ws.tag]) {
  97. // this.cbList[result.ws.tag](result)
  98. // delete this.cbList[result.ws.tag]
  99. // }
  100. // // if (temp.team) {
  101. // // GameDataCenter.sevTeam = temp.team
  102. // // EventMng.emit(SevBackEvent.UP_SEV_TEAM)
  103. // // }
  104. // // if (params.ws.pve) {
  105. // // for (const uuid in params.ws.pve.list) {
  106. // // cc.game.emit(ServerBridgeEvent.ServerBridgeEvent, uuid, params.ws.pve.list[uuid])
  107. // // }
  108. // // }
  109. // }
  110. // closeConnect(key: string) {
  111. // if (this.ws[key]) {
  112. // this.ws[key].close()
  113. // delete this.ws[key]
  114. // }
  115. // }
  116. }