Mgobe.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. let mogobe = {
  2. // MGOBE 游戏信息
  3. gameId: "obg-ojj4ptiw",
  4. secretKey: "bf030e95ebde217416f13efc5a0c73532544f783",
  5. url: "ojj4ptiw.wxlagame.com",
  6. cacertNativeUrl: '',
  7. // 默认匹配 Code
  8. matchCode: "match-i2iytgmb",
  9. openId: 'openid_test' + Math.random(), //自定义的用户唯一ID
  10. roomId:'',
  11. Player:'',
  12. /**
  13. * 判断 MGOBE SDK 是否初始化完成
  14. */
  15. isInited() {
  16. // 初始化成功后才有玩家ID
  17. return !!MGOBE.Player && !!MGOBE.Player.id;
  18. },
  19. /**
  20. * 初始化 MGOBE SDK
  21. * @param gameInfo
  22. * @param config
  23. * @param callback
  24. */
  25. // initSDK(gameId, secretKey, url, cacertNativeUrl, callback) {
  26. initSDK(callback,listener) {
  27. // 如果已经初始化,直接回调成功
  28. if (this.isInited()) {
  29. return callback && callback({code: MGOBE.ErrCode.EC_OK});
  30. }
  31. const defaultGameInfo = {
  32. gameId: this.gameId,
  33. openId: this.openId,
  34. secretKey: this.secretKey,
  35. };
  36. const defaultConfig = {
  37. url: this.url,
  38. isAutoRequestFrame: true,
  39. cacertNativeUrl: this.cacertNativeUrl,
  40. };
  41. // MGOBE.DebuggerLog.enable = true;
  42. // if (cc.sys.isNative) {
  43. // MGOBE.DebuggerLog.enable = false;
  44. // }
  45. //是否开启调试
  46. MGOBE.DebuggerLog.enable = false;
  47. // 初始化
  48. let _self = this;
  49. MGOBE.Listener.init(defaultGameInfo, defaultConfig, event => {
  50. if (event.code === MGOBE.ErrCode.EC_OK) {
  51. console.log("初始化 SDK 成功");
  52. _self.player = MGOBE.Player;
  53. _self.room = new MGOBE.Room();
  54. MGOBE.Listener.add(_self.room);
  55. // 设置默认广播
  56. _self.room.onJoinRoom = _self.onJoinRoom.bind(_self);
  57. _self.room.onLeaveRoom = _self.onLeaveRoom.bind(_self);
  58. _self.room.onRecvFromClient = _self.onRecvFromClient.bind(_self);
  59. _self.room.onRecvFrame = _self.onRecvFrame.bind(_self);
  60. _self.room.onStartFrameSync = _self.onStartFrameSync.bind(_self);
  61. _self.room.onStopFrameSync = _self.onStopFrameSync.bind(_self);
  62. _self.room.onRecvFromGameSvr = _self.onRecvFromGameSvr.bind(_self);
  63. } else {
  64. console.log(`初始化 SDK 失败,错误码:${event.code}`);
  65. }
  66. callback && callback(event.code);
  67. });
  68. },
  69. // SDK 发送房间消息
  70. sendMessage(msg) {
  71. console.log(`正在发送房间消息`);
  72. const sendToClientPara = {
  73. recvPlayerList: [],
  74. recvType: MGOBE.types.RecvType.ROOM_OTHERS,
  75. msg,
  76. };
  77. this.room.sendToClient(sendToClientPara, event => {
  78. if (event.code === MGOBE.ErrCode.EC_OK) {
  79. console.log(`发送房间消息成功`);
  80. } else {
  81. console.log(`发送房间消息失败,错误码:${event.code}`);
  82. }
  83. });
  84. },
  85. // SDK 发送实时服务器消息
  86. sendToGameSvr(cmd) {
  87. console.log(`正在发送房间消息`);
  88. const sendToGameSvrPara = {
  89. data: {
  90. cmd,
  91. },
  92. };
  93. this.room.sendToGameSvr(sendToGameSvrPara, event => {
  94. if (event.code === MGOBE.ErrCode.EC_OK) {
  95. console.log(`发送实时服务器消息成功`);
  96. } else {
  97. console.log(`发送实时服务器消息失败,错误码:${event.code}`);
  98. }
  99. });
  100. },
  101. // SDK 发送帧消息
  102. sendFrame(cmd) {
  103. console.log(`正在发送帧消息`);
  104. const sendFramePara = {
  105. data: {
  106. cmd,
  107. },
  108. };
  109. this.room.sendFrame(sendFramePara, event => {
  110. if (event.code === MGOBE.ErrCode.EC_OK) {
  111. console.log(`发送帧消息成功`);
  112. } else {
  113. console.log(`发送帧消息失败,错误码:${event.code}`);
  114. }
  115. });
  116. },
  117. // SDK 开始帧同步
  118. startFrameSync(callback) {
  119. console.log(`正在开始帧同步`);
  120. this.room.startFrameSync({}, event => {
  121. if (event.code === MGOBE.ErrCode.EC_OK) {
  122. console.log(`开始帧同步成功`);
  123. callback && callback();
  124. } else {
  125. console.log(`开始帧同步失败,错误码:${event.code}`);
  126. }
  127. });
  128. },
  129. // SDK 停止帧同步
  130. stopFrameSync(success) {
  131. console.log(`正在停止帧同步`);
  132. this.room.stopFrameSync({}, event => {
  133. if (event.code === MGOBE.ErrCode.EC_OK) {
  134. console.log(`停止帧同步成功`);
  135. success && success();
  136. } else {
  137. console.log(`停止帧同步失败,错误码:${event.code}`);
  138. }
  139. });
  140. },
  141. onJoinRoom: function (event) {
  142. console.log("新玩家加入" + event.data.joinPlayerId);
  143. },
  144. onLeaveRoom: function (event) {
  145. console.log("玩家退出" + event.data.leavePlayerId);
  146. },
  147. onRecvFromClient: function (event) {
  148. console.log("新消息" + event.data.msg);
  149. },
  150. onRecvFrame: function (event) {
  151. // this.sendFrame();
  152. // console.log("帧广播", event.data.frame);
  153. // if (event.data.frame.items && event.data.frame.items.length > 0) {
  154. // //console.log("帧广播" + JSON.stringify(event.data.frame.items));
  155. // console.log("帧广播", event.data.frame);
  156. // }
  157. },
  158. onStartFrameSync: function (event) {
  159. this.synced = true;
  160. console.log("开始帧同步\n");
  161. },
  162. onStopFrameSync: function (event) {
  163. this.synced = false;
  164. console.log("停止帧同步\n");
  165. },
  166. onRecvFromGameSvr: function (event) {
  167. // console.log("新自定义服务消息", event);
  168. //console.log("新自定义服务消息" + JSON.stringify(event));
  169. },
  170. // SDK 随机匹配
  171. matchPlayers(callback) {
  172. let matchCode = this.matchCode;
  173. if (!matchCode) {
  174. return console.log(`请输入正确的匹配 Code`);
  175. }
  176. this.lockSubmit = true;
  177. this.timerStartMatching = setInterval(() => console.log(`正在随机匹配,请稍等。`), 1000);
  178. console.log(`正在随机匹配,匹配Code:${matchCode}。请稍等,默认超时时间为 10 秒。`);
  179. // 注意:这里没有使用匹配属性,如果匹配规则中有设置匹配属性,这里需要做调整
  180. const matchAttributes = [];
  181. const playerInfo = {
  182. name:this.userName,
  183. customPlayerStatus: 0,
  184. customProfile:"",
  185. matchAttributes,
  186. };
  187. const matchPlayersPara = {
  188. matchCode,
  189. playerInfo,
  190. };
  191. this.room.initRoom();
  192. this.room.matchPlayers(matchPlayersPara, event =>
  193. {
  194. this.lockSubmit = false;
  195. clearInterval(this.timerStartMatching);
  196. if (event.code === MGOBE.ErrCode.EC_OK)
  197. {
  198. console.log(`随机匹配成功,房间ID:${event.data.roomInfo.id}`);
  199. this.roomId = event.data.roomInfo.id;
  200. callback && callback();
  201. } else {
  202. console.log(`随机匹配失败,错误码:${event.code}`);
  203. }
  204. });
  205. },
  206. //取消随机匹配
  207. cancelPlayerMatch(callback)
  208. {
  209. this.timerEndMatching = setInterval(() => console.log(`正在取消匹配,请稍等。`), 1000);
  210. const cancelMatchPara = {
  211. matchType: MGOBE.ENUM.MatchType.PLAYER_COMPLEX,
  212. };
  213. this.room.cancelPlayerMatch(cancelMatchPara,event =>
  214. {
  215. clearInterval(this.timerStartMatching);
  216. clearInterval(this.timerEndMatching);
  217. if (event.code === MGOBE.ErrCode.EC_OK)
  218. {
  219. console.log('取消匹配成功');
  220. callback && callback();
  221. } else {
  222. console.log(`取消匹配失败,错误码:${event.code}`);
  223. }
  224. });
  225. }
  226. };
  227. module.exports = mogobe;