import BGS from '../../js/bmobgamesdk/bgsapi';//根據(jù)你自己的存放路徑更改 let model = BGS.instance; model.Init('3f729baax0', 'ws://139.159.220.251:29175', function (succ, msg) { if (succ) { // 用bmob小程序sdk進(jìn)行登錄注冊 // _getUser(listener); } else{ // 提醒失敗,并給重新init的按鈕 } });
import BGS from '../../js/bmobgamesdk/bgsapi';//根據(jù)你自己的存放路徑更改 let model = BGS.instance; var userId = Bmob.User.current().id; model.CreateRoom(userId, 2, function(isOK, res) { console.log("res >", res); if (isOK) { var roomInfo = res.roomInfo; console.log('對戰(zhàn)開房成功', _data); // 創(chuàng)建房間成功,跳轉(zhuǎn)游戲房間頁面,別忘了把房間信息roomInfo傳遞過去 } else { common.showModal('對戰(zhàn)開房失敗,' + res); } });
{ "address": "a.b.c.d", // 服務(wù)器ip "roomInfo": { "ports": { "websocket": efgh // 服務(wù)器端口 }, "rid": xxx, // 房間id "joinKey": yyy // 房間密匙 } }
這樣,你就獲得了初始化sdk的第二個參數(shù),是 ws://a.b.c.d:efgh 這樣的格式
import BGS from '../../js/bmobgamesdk/bgsapi';//根據(jù)你自己的存放路徑更改 let model = BGS.instance; if (this.isConnected) return; t('連接房間'); this.mRoomActListener = this.onRoomAction.bind(this); this.mOfflineListener = this.onOffline.bind(this); model.RegistRoomActListener(this.mRoomActListener);// 注冊系統(tǒng)通知監(jiān)聽,詳細(xì)參考官網(wǎng)下載的demo model.RegistOfflineListener(this.mOfflineListener);// 注冊掉線通知監(jiān)聽 let emptyFun = function() {}; model.SetGameRuntimeListeners( emptyFun,//這三個監(jiān)聽器本游戲中沒有涉及 emptyFun, emptyFun, this.onTransfer.bind(this),// 玩家間交互信息的監(jiān)聽器 this.onCloudNotify.bind(this)//云端代碼通知的監(jiān)聽器 ); // 加入房間 // 這里的roomData就是創(chuàng)建房間時讓你保存的roomInfo啦 model.JoinRoom(that.roomData.rid, that.roomData.joinKey, userId, model.get('seatKey'), function(isOK, data) { if (isOK) { common.toast('加入房間成功'); console.log("房間信息:", data);
let playerCount = data.playerCount, no = data.no, isPlaying = data.isPlaying, players = data.players, masterId = data.master; // 根據(jù)返回的房間信息做些保存或處理,這里不詳細(xì)寫出來,大家根據(jù)自己的情況靈活變通... if (data.seatKey) model.set('seatKey', data.seatKey); that.isConnected = true; return; }
//加入房間失敗 if (data.indexOf("204") > -1) data = "房間已關(guān)閉"; else if (data.indexOf("206") > -1) data = "房間已滿員"; else if (data.indexOf("201") > -1) data = "未知錯誤"; else if (data.indexOf("203") > -1) data = "沒有登陸"; else if (data.indexOf("208") > -1) data = "游戲中"; else if (data.indexOf("204") > -1) data = "房間不存在"; else if (data.indexOf("202") > -1) data = "參數(shù)錯誤"; console.log('加入房間失敗:', data); }.bind(this)); // 收到客戶端的回調(diào) onTransfer(no, res) { console.log("收到客戶端的回調(diào):" + no, res) let flag = res.shift(); switch (flag) { case 1: // 下棋 t('收到客戶端的下棋數(shù)據(jù)'); t(res); break; case 2: // 被請求悔棋 // ... } }, // 收到云端代碼服務(wù)端的回調(diào)
onCloudNotify(notify) { notify = JSON.parse(model.bytesToString(notify, 0, notify.length)); console.log("收到服務(wù)器的回調(diào):", notify); // ... },
// 玩家之間交互數(shù)據(jù),上面的onTransfer會收到對方發(fā)送的 // 要以byte數(shù)組形式,一般把數(shù)組的第一位作為自定義交互類型flag,后面的為要交互的數(shù)據(jù) model.SendTransferToAllExceptSelf([1, ...]); // sdk特別提供了把string和byte數(shù)組互轉(zhuǎn)的方法 model.stringToBytes('string'); // string轉(zhuǎn)byte[] model.model.bytesToString(bs, 0, bs.length); // byte[]轉(zhuǎn)string // 調(diào)用云端代碼,參數(shù)為云端代碼方法名 model.CloudAction('Ready');
//生命周期函數(shù)--監(jiān)聽頁面卸載 onUnload: function() { if (that.battle && that.isConnected) { // model.SendTransferToAllExceptSelf([4]);// 向?qū)Ψ桨l(fā)送離開信息 model.UnregistOfflineListener(this.mOfflineListener); model.StopGameRuntimeListener(); this.mOfflineListener = null; model.QuitRoom(); } }
可加官方客服QQ:2967459363
微信小程序搜索<棋頭并進(jìn)>,體驗(yàn)此款下棋微信小游戲