aNetLib.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // var qqgamesdk = require('qqgamesdk');
  2. var qqgamesdk = require('qqgame-sdk-0.4.2.min');
  3. window.qqNetwork = {};
  4. // load sdk
  5. qqNetwork.qqSdkInitFlag = false;
  6. if (!cc.sys.isNative) {
  7. }
  8. qqNetwork.room2Game = {
  9. channelCreated: "channelCreated", // 通道创建,大厅到游戏
  10. gameData: "gameData", // 数据传输,游戏到大厅或者大厅到游戏
  11. frameData: "frameData",
  12. gameStart: "gameStart", // 游戏开始,大厅到游戏
  13. network: "network",
  14. leave: "leave",
  15. voice: "voice",
  16. gameReset: "gameReset",// 游戏重置,大厅到游戏
  17. gameEnd: 'game_end',//有一端退出?
  18. playerEnter: 'playerEnter',//玩家从新进入游戏
  19. };
  20. qqNetwork.game2Room = {
  21. gameInit: "gameInit", // 游戏初始化,游戏到大厅
  22. gameData: "gameData", // 游戏数据传输,游戏到大厅或者大厅到游戏
  23. gameReady: "gameReady", // 游戏ready,游戏到大厅
  24. gameResult: "gameResult", //结算信息,游戏到大厅
  25. frameData: "frameData",
  26. leave: "leave", // 退出,游戏到大厅
  27. };
  28. qqNetwork.msgProcess = function (msgName, msgData) {
  29. if (typeof msgData === "string") {
  30. if (msgData != "gameStart") {
  31. msgData = JSON.parse(msgData);
  32. }
  33. }
  34. switch (msgName) {
  35. case qqNetwork.room2Game.channelCreated:
  36. var paramFlag1 = false;
  37. var paramFlag2 = false;
  38. if (msgData.playerInfos[0].headurl && msgData.playerInfos[0].headurl.indexOf("?") !== -1) {
  39. paramFlag1 = true;
  40. }
  41. if (msgData.playerInfos[1].headurl && msgData.playerInfos[1].headurl.indexOf("?") !== -1) {
  42. paramFlag2 = true;
  43. }
  44. msgData.playerInfos[0].headurl = decodeURI(msgData.playerInfos[0].headurl);
  45. msgData.playerInfos[1].headurl = decodeURI(msgData.playerInfos[1].headurl);
  46. if (paramFlag1) {
  47. msgData.playerInfos[0].headurl = msgData.playerInfos[0].headurl + "&aa=aa.jpg";
  48. } else {
  49. msgData.playerInfos[0].headurl = msgData.playerInfos[0].headurl + "?aa=aa.jpg";
  50. }
  51. if (paramFlag2) {
  52. msgData.playerInfos[1].headurl = msgData.playerInfos[1].headurl + "&aa=aa.jpg";
  53. } else {
  54. msgData.playerInfos[1].headurl = msgData.playerInfos[1].headurl + "?aa=aa.jpg";
  55. }
  56. this.ChannelData = msgData;
  57. this.isHasChannelData = true;
  58. // console.log('channelCreated', msgData)
  59. // if(this.InitIndex == 0){
  60. // qqNetwork.send(qqNetwork.game2Room.gameReady, {});
  61. // }
  62. // this.InitIndex = 1;
  63. break;
  64. case qqNetwork.room2Game.gameReset:
  65. console.log("aNetLib 重置游戏");
  66. // qqNetwork._init();
  67. // var NetworkSocket = cc.find('Hero').getComponent("NetworkSocket");
  68. // NetworkSocket.startGame();
  69. this.isHasChannelData = false;
  70. this.send(qqNetwork.game2Room.gameInit);//{ openFraming: 1, fpsExpect: 5 }
  71. cc.director.loadScene("camera_Mutiplayers");
  72. break;
  73. }
  74. if (this.msgCb) this.msgCb(msgName, msgData);
  75. };
  76. qqNetwork._init = function () {
  77. this.qqSdkInitFlag = true;
  78. if (this.debugFlag) {
  79. return;
  80. }
  81. // console.log('EnterInitFunction');
  82. if (window.qqgame) {
  83. // console.log('QQ Init');
  84. window.qqgame.onMessage(function (msgName, msgData) {
  85. this.msgProcess(msgName, msgData)
  86. }.bind(this));
  87. }
  88. //游戏开始gameInit
  89. this.isHasChannelData = false;
  90. this.send(this.game2Room.gameInit);//{ openFraming: 1, fpsExpect: 5 }
  91. };
  92. qqNetwork.connect = function (ip, port, cb) {
  93. port = port || 8181;
  94. // 测试用,正在大厅用不上
  95. console.log('Socket Connet');
  96. var socketUrl = port !== 0 ? "ws://" + ip + ":" + port : "ws://" + ip;
  97. if (this.web_socket) {
  98. this.web_socket.close();
  99. this.web_socket = null;
  100. }
  101. this.web_socket = new WebSocket(socketUrl);
  102. // this.web_socket.binaryType = "arraybuffer";
  103. this.web_socket.onmessage = function (event) {
  104. var data = JSON.parse(event.data);
  105. var msgName = data["eID"];
  106. var msgData = data["ePara"];
  107. this.msgProcess(msgName, msgData);
  108. // console.log("receive data:" + event.data);
  109. }.bind(this);
  110. this.web_socket.onopen = function (event) {
  111. cc.log("onopen------------");
  112. if (cb) cb();
  113. if (this.msgCb) this.msgCb("online", {});
  114. }.bind(this);
  115. this.web_socket.onclose = function (event) {
  116. cc.log("onclose------------");
  117. this.web_socket = null;
  118. if (this.msgCb) this.msgCb("offline", {});
  119. }.bind(this);
  120. this.web_socket.onerror = function (event) {
  121. cc.log("onerror------------");
  122. if (this.msgCb) this.msgCb("offline", {});
  123. }.bind(this);
  124. };
  125. qqNetwork.send = function (reqName, originData) {
  126. var data = {
  127. "eID": reqName,
  128. "ePara": originData,
  129. };
  130. // console.log("send data:" + JSON.stringify(data));
  131. if (!this.debugFlag) {
  132. if (this.qqSdkInitFlag) {
  133. window.qqgame.invoke(reqName, originData);
  134. } else {
  135. setTimeout(function () {
  136. this.send(reqName, originData);
  137. }.bind(this), 200);
  138. }
  139. } else {
  140. if (!this.web_socket) {
  141. console.error("debug mode need connect websocket");
  142. return;
  143. }
  144. this.web_socket.send(JSON.stringify(data));
  145. }
  146. };
  147. qqNetwork.enableDebug = function () {
  148. // this.InitIndex = 0;
  149. this.debugFlag = false;
  150. console.log('debugFlag==' + this.debugFlag);
  151. };
  152. qqNetwork.regMsgCallback = function (cb) {
  153. qqNetwork.msgCb = cb;
  154. };
  155. qqNetwork._init();
  156. // window.network = qqNetwork;