Mgobe.js 8.2 KB

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