Socket.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. this._WebSocket = new WebSocket("ws://"+url+":"+port);
  100. this._WebSocket.binaryType = "arraybuffer";
  101. this._WebSocket.onopen = (event)=>{
  102. this.m_SocketID = getFreeSocketID();
  103. _gLinkArr[this.m_SocketID] = this;
  104. if(LOG_NET_DATA) console.log("网络连接成功!", this.m_SocketID);
  105. this.mSocketSink.onSocketLink(event);
  106. };
  107. this._WebSocket.onmessage = (event)=>{
  108. if(_gLinkArr[this.m_SocketID] == null) return;
  109. this.mSocketSink.onSocketData(event.data);
  110. };
  111. this._WebSocket.onclose = (event)=>{
  112. if(this.readyState != 1)return;
  113. if(_gLinkArr[this.m_SocketID] == null) return;
  114. if(LOG_NET_DATA)console.log('关闭网络!');
  115. var SocketID = this._WebSocket.m_SocketID;
  116. _gLinkArr[SocketID] = null;
  117. this._WebSocket.close();
  118. this.mSocketSink.onSocketShut();
  119. //g_NetQuene.push(NET_QUENE_CLOSE, this.m_Socket, event);
  120. };
  121. this._WebSocket.onerror = (event)=>{
  122. if(LOG_NET_DATA)console.log("网络异常:",this.m_Queue[0].m_Prama2);
  123. var SocketID = this._WebSocket.m_SocketID;
  124. _gLinkArr[SocketID] = null;
  125. this.mSocketSink.onSocketError(0);
  126. this._WebSocket = null;
  127. // g_NetQuene.push(NET_QUENE_ERROR, this.m_Socket, event);
  128. };
  129. //g_NetQuene.push(NET_QUENE_CONNECT, this, url, port);
  130. },
  131. send :function(data){
  132. if (this._WebSocket == null)return;
  133. this._WebSocket.send(data);
  134. //g_NetQuene.push(NET_QUENE_SEND, this, data);
  135. },
  136. disconnect :function(){
  137. if(LOG_NET_DATA)console.log("主动关闭");
  138. if(this._WebSocket != null){
  139. var WebSocket = this._WebSocket;
  140. if(_gLinkArr[WebSocket.m_SocketID]) this.mSocketSink.onSocketShut();
  141. _gLinkArr[WebSocket.m_SocketID] = null;
  142. WebSocket.close();
  143. }
  144. // g_NetQuene.push(NET_QUENE_DISCONNECT, this);
  145. },
  146. //////////////////////////////////////////////////////
  147. //WebSocket
  148. onopen :function(event){
  149. this.m_SocketID = getFreeSocketID();
  150. _gLinkArr[this.m_SocketID] = this;
  151. if(LOG_NET_DATA) console.log("网络连接成功!", this.m_SocketID);
  152. this.mSocketSink.onSocketLink(event);
  153. //g_NetQuene.push(NET_QUENE_OPEN, this.m_Socket, event);
  154. },
  155. //监听消息
  156. onmessage :function(event){
  157. if(_gLinkArr[this.m_SocketID] == null) return;
  158. this.mSocketSink.onSocketData(event.data);
  159. //g_NetQuene.push(NET_QUENE_MESSAGE, this.m_Socket, event);
  160. },
  161. //监听Socket的关闭
  162. onclose :function(event){
  163. if(this.readyState != 1)return;
  164. if(_gLinkArr[this.m_SocketID] == null) return;
  165. if(LOG_NET_DATA)console.log('关闭网络!');
  166. var SocketID = this._WebSocket.m_SocketID;
  167. _gLinkArr[SocketID] = null;
  168. this._WebSocket.close();
  169. this.mSocketSink.onSocketShut();
  170. //g_NetQuene.push(NET_QUENE_CLOSE, this.m_Socket, event);
  171. },
  172. //监听Socket的异常
  173. onerror :function(event){
  174. if(LOG_NET_DATA)console.log("网络异常:",this.m_Queue[0].m_Prama2);
  175. var SocketID = this._WebSocket.m_SocketID;
  176. _gLinkArr[SocketID] = null;
  177. this.mSocketSink.onSocketError(0);
  178. this._WebSocket = null;
  179. // g_NetQuene.push(NET_QUENE_ERROR, this.m_Socket, event);
  180. },
  181. });