Socket.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //调用类型
  2. // var NET_QUENE_UNKONW = 0;
  3. // var NET_QUENE_CONNECT = 1;
  4. // var NET_QUENE_OPEN = 2;
  5. // var NET_QUENE_MESSAGE = 3;
  6. // var NET_QUENE_CLOSE = 4;
  7. // var NET_QUENE_ERROR = 5;
  8. // var NET_QUENE_DISCONNECT = 6;
  9. // var NET_QUENE_SEND = 7;
  10. //网络类型
  11. // var NetQueueType = cc.Class({
  12. // ctor :function () {
  13. // this.m_Type = arguments[0];
  14. // this.m_Prama1 = arguments[1];
  15. // this.m_Prama2 = arguments[2];
  16. // this.m_Prama3 = arguments[3];
  17. // },
  18. // });
  19. // //网络列队
  20. // var NetQueue = cc.Class({
  21. // ctor :function () {
  22. // this.m_Looping = false;
  23. // this.m_Queue = new Array();
  24. // },
  25. // push :function (Type, SocketHook, Prama2, Prama3) {
  26. // if(!this.m_Looping){
  27. // this.m_Looping = true;
  28. // setInterval(this.process.bind(this), 1);//1000为1秒钟
  29. // }
  30. // this.m_Queue.push(new NetQueueType(Type, SocketHook, Prama2, Prama3));
  31. // },
  32. // process :function () {
  33. // if (this.m_Queue.length <= 0)return;
  34. // if (this.m_Queue[0].m_Prama1==null)return;
  35. // switch (this.m_Queue[0].m_Type) {
  36. // case NET_QUENE_CONNECT:
  37. // var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket;
  38. // this.m_Queue[0].m_Prama1.mWebSocket = new WebSocket("ws://"+this.m_Queue[0].m_Prama2+":"+this.m_Queue[0].m_Prama3);
  39. // this.m_Queue[0].m_Prama1.mWebSocket.binaryType = "arraybuffer";
  40. // this.m_Queue[0].m_Prama1.mWebSocket.onopen = this.m_Queue[0].m_Prama1.onopen;
  41. // this.m_Queue[0].m_Prama1.mWebSocket.onmessage = this.m_Queue[0].m_Prama1.onmessage;
  42. // this.m_Queue[0].m_Prama1.mWebSocket.onclose = this.m_Queue[0].m_Prama1.onclose;
  43. // this.m_Queue[0].m_Prama1.mWebSocket.onerror = this.m_Queue[0].m_Prama1.onerror;
  44. // this.m_Queue[0].m_Prama1.mWebSocket.m_Socket = this.m_Queue[0].m_Prama1;
  45. // break;
  46. // case NET_QUENE_OPEN:
  47. // this.m_Queue[0].m_Prama1.mSocketSink.onSocketLink(this.m_Queue[0].m_Prama2);
  48. // break;
  49. // case NET_QUENE_MESSAGE:
  50. // this.m_Queue[0].m_Prama1.mSocketSink.onSocketData(this.m_Queue[0].m_Prama2.data);
  51. // break;
  52. // case NET_QUENE_CLOSE:
  53. // if(LOG_NET_DATA)console.log('关闭网络!');
  54. // var SocketID = this.m_Queue[0].m_Prama1.mWebSocket.m_SocketID;
  55. // _gLinkArr[SocketID] = null;
  56. // this.m_Queue[0].m_Prama1.mWebSocket.close();
  57. // this.m_Queue[0].m_Prama1.mSocketSink.onSocketShut();
  58. // break;
  59. // case NET_QUENE_ERROR:
  60. // if(LOG_NET_DATA)console.log("网络异常:",this.m_Queue[0].m_Prama2);
  61. // var SocketID = this.m_Queue[0].m_Prama1.mWebSocket.m_SocketID;
  62. // _gLinkArr[SocketID] = null;
  63. // this.m_Queue[0].m_Prama1.mSocketSink.onSocketError(0);
  64. // this.m_Queue[0].m_Prama1.mWebSocket = null;
  65. // break;
  66. // case NET_QUENE_DISCONNECT:
  67. // if(LOG_NET_DATA)console.log("主动关闭");
  68. // if(this.m_Queue[0].m_Prama1.mWebSocket != null){
  69. // var WebSocket = this.m_Queue[0].m_Prama1.mWebSocket;
  70. // if(_gLinkArr[WebSocket.m_SocketID]) this.m_Queue[0].m_Prama1.mSocketSink.onSocketShut();
  71. // _gLinkArr[WebSocket.m_SocketID] = null;
  72. // WebSocket.close();
  73. // }
  74. // break;
  75. // case NET_QUENE_SEND:
  76. // if (this.m_Queue[0].m_Prama1.mWebSocket == null)break;
  77. // this.m_Queue[0].m_Prama1.mWebSocket.send(this.m_Queue[0].m_Prama2);
  78. // break;
  79. // default:
  80. // if(LOG_NET_DATA)console.log("无效WebScoket包!");
  81. // }
  82. // this.m_Queue.shift();
  83. // }
  84. // });
  85. // var g_NetQuene = new NetQueue();
  86. cc.CSocket = cc.Class({
  87. ctor: function () {
  88. this.mSocketSink = arguments[0];
  89. //this.mWebSocket = null;
  90. this._WebSocket = null;
  91. },
  92. isAlive: function () {
  93. if (this._WebSocket == null) return false;
  94. return this._WebSocket.readyState == 1;
  95. },
  96. connect: function (url, port) {
  97. var self = this;
  98. var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket;
  99. if (window.OPEN_DOMAIN_NET&&(cc.sys.isNative!= cc.sys.os)) {
  100. // if (cc.sys.OS_ANDROID == cc.sys.os) {
  101. // this._WebSocket = new WebSocket("wss://" + url);
  102. // } else {
  103. this._WebSocket = new WebSocket("wss://" + url + ":" + port);
  104. // }
  105. } else {
  106. this._WebSocket = new WebSocket("ws://" + url + ":" + port);
  107. }
  108. this._WebSocket.binaryType = "arraybuffer";
  109. this._WebSocket.onopen = (event) => {
  110. this.m_SocketID = getFreeSocketID();
  111. _gLinkArr[this.m_SocketID] = this;
  112. if (LOG_NET_DATA) console.log("网络连接成功!", this.m_SocketID);
  113. this.mSocketSink.onSocketLink(event);
  114. };
  115. this._WebSocket.onmessage = (event) => {
  116. if (_gLinkArr[this.m_SocketID] == null) return;
  117. this.mSocketSink.onSocketData(event.data);
  118. };
  119. this._WebSocket.onclose = (event) => {
  120. if (this.readyState != 1) return;
  121. if (_gLinkArr[this.m_SocketID] == null) return;
  122. if (LOG_NET_DATA) console.log('关闭网络!');
  123. var SocketID = this._WebSocket.m_SocketID;
  124. _gLinkArr[SocketID] = null;
  125. this._WebSocket.close();
  126. this.mSocketSink.onSocketShut();
  127. //g_NetQuene.push(NET_QUENE_CLOSE, this.m_Socket, event);
  128. };
  129. this._WebSocket.onerror = (event) => {
  130. if (LOG_NET_DATA) console.log("网络异常:", this.m_Queue[0].m_Prama2);
  131. var SocketID = this._WebSocket.m_SocketID;
  132. _gLinkArr[SocketID] = null;
  133. this.mSocketSink.onSocketError(0);
  134. this._WebSocket = null;
  135. // g_NetQuene.push(NET_QUENE_ERROR, this.m_Socket, event);
  136. };
  137. //g_NetQuene.push(NET_QUENE_CONNECT, this, url, port);
  138. },
  139. send: function (data) {
  140. if (this._WebSocket == null) return;
  141. this._WebSocket.send(data);
  142. //g_NetQuene.push(NET_QUENE_SEND, this, data);
  143. },
  144. disconnect: function () {
  145. if (LOG_NET_DATA) console.log("主动关闭");
  146. if (this._WebSocket != null) {
  147. var WebSocket = this._WebSocket;
  148. if (_gLinkArr[WebSocket.m_SocketID]) this.mSocketSink.onSocketShut();
  149. _gLinkArr[WebSocket.m_SocketID] = null;
  150. WebSocket.close();
  151. }
  152. // g_NetQuene.push(NET_QUENE_DISCONNECT, this);
  153. },
  154. //////////////////////////////////////////////////////
  155. //WebSocket
  156. onopen: function (event) {
  157. this.m_SocketID = getFreeSocketID();
  158. _gLinkArr[this.m_SocketID] = this;
  159. if (LOG_NET_DATA) console.log("网络连接成功!", this.m_SocketID);
  160. this.mSocketSink.onSocketLink(event);
  161. //g_NetQuene.push(NET_QUENE_OPEN, this.m_Socket, event);
  162. },
  163. //监听消息
  164. onmessage: function (event) {
  165. if (_gLinkArr[this.m_SocketID] == null) return;
  166. this.mSocketSink.onSocketData(event.data);
  167. //g_NetQuene.push(NET_QUENE_MESSAGE, this.m_Socket, event);
  168. },
  169. //监听Socket的关闭
  170. onclose: function (event) {
  171. if (this.readyState != 1) return;
  172. if (_gLinkArr[this.m_SocketID] == null) return;
  173. if (LOG_NET_DATA) console.log('关闭网络!');
  174. var SocketID = this._WebSocket.m_SocketID;
  175. _gLinkArr[SocketID] = null;
  176. this._WebSocket.close();
  177. this.mSocketSink.onSocketShut();
  178. //g_NetQuene.push(NET_QUENE_CLOSE, this.m_Socket, event);
  179. },
  180. //监听Socket的异常
  181. onerror: function (event) {
  182. if (LOG_NET_DATA) console.log("网络异常:", this.m_Queue[0].m_Prama2);
  183. var SocketID = this._WebSocket.m_SocketID;
  184. _gLinkArr[SocketID] = null;
  185. this.mSocketSink.onSocketError(0);
  186. this._WebSocket = null;
  187. // g_NetQuene.push(NET_QUENE_ERROR, this.m_Socket, event);
  188. },
  189. });