Commit ae481e4c authored by 范雪寒's avatar 范雪寒

feat:联机工具类

parent b17cfbc7
import { RandomInt } from "../script/util";
export class NetworkHelper {
_eventListeners: any = {};
ctor() { }
on(eventName, func) {
this._eventListeners[eventName] = func;
}
async init() {
await this.initRoom();
return await this.joinRoom();
}
async startGame() {
await this.startFrameSync();
await this.closeRoom();
}
async stopGame() {
await this.stopFrameSync();
await this.leaveRoom();
}
getRoomInfo() {
return this.room.roomInfo;
}
room: MGOBE.Room;
initRoom() {
return new Promise((resolve, reject) => {
const gameId = "obg-d75cgddl"; //替换为控制台上的 游戏 ID
const secretKey = "ee8c7260319780749d5f13bfe1420a4d083cbc77"; //替换为控制台上的 游戏 Key
const serverUrl = "d75cgddl.wxlagame.com"; //替换为控制台上的 域名
const gameInfo = {
gameId: gameId,
openId: "openid_test" + Math.random(), //自定义的用户唯一ID
secretKey: secretKey
};
const config = {
url: serverUrl,
reconnectMaxTimes: 5,
reconnectInterval: 1000,
resendInterval: 1000,
resendTimeout: 10000,
cacertNativeUrl: ""
};
// MGOBE.DebuggerLog.enable = true;
// 如果是原生平台,则加载 Cert 证书,否则会提示 WSS 错误
// if (cc.sys.isNative) {
// config.cacertNativeUrl = cc.loader.md5Pipe && cc.ENGINE_VERSION < "2.4.0" ?
// cc.loader.md5Pipe.transformURL(this.cacertFile.nativeUrl) :
// this.cacertFile.nativeUrl;
// }
MGOBE.Listener.init(gameInfo, config, event => {
console.log(event);
if (event.code !== 0) {
cc.log("初始化失败");
reject();
return;
}
cc.log("初始化成功");
this.room = new MGOBE.Room();
MGOBE.Listener.add(this.room);
this.room.onJoinRoom = this.onJoinRoom.bind(this);
this.room.onLeaveRoom = this.onLeaveRoom.bind(this);
this.room.onRecvFromClient = this.onRecvFromClient.bind(this);
this.room.onRecvFrame = this.onRecvFrame.bind(this);
this.room.onStartFrameSync = this.onStartFrameSync.bind(this);
this.room.onStopFrameSync = this.onStopFrameSync.bind(this);
this.room.onRecvFromGameSvr = this.onRecvFromGameSvr.bind(this);
resolve(null);
});
});
}
joinRoom() {
return new Promise((resolve, reject) => {
const playerName = "Tom" + Math.random();
const playerInfo = {
name: playerName,
customPlayerStatus: 0,
customProfile: JSON.stringify({
x: RandomInt(-640, 640),
y: RandomInt(-360, 360)
}),
};
const matchRoomPara = {
playerInfo,
maxPlayers: 5,
roomType: "1"
};
this.room.matchRoom(matchRoomPara, event => {
console.log(event);
if (event.code === 0) {
cc.log("匹配成功");
const player = event.data.roomInfo.playerList.find(player => player.name == playerName);
resolve(player.id);
} else {
cc.log("匹配失败");
reject();
}
});
});
}
leaveRoom() {
return new Promise((resolve, reject) => {
this.room.leaveRoom({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("退房成功", this.room.roomInfo.id);
this.room.initRoom();
resolve(null);
}
});
});
}
closeRoom() {
return new Promise((resolve, reject) => {
this.room.changeRoom({ isForbidJoin: false }, (event) => {
console.log('关门')
console.log(event);
resolve(null);
});
});
}
sendMessage() {
const sendToClientPara = {
recvType: MGOBE.ENUM.RecvType.ROOM_ALL,
recvPlayerList: [],
msg: "hello" + Math.random()
};
this.room.sendToClient(sendToClientPara, event => console.log(event));
}
startFrameSync() {
return new Promise((resolve, reject) => {
this.room.startFrameSync({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
resolve(null);
} else {
reject();
}
});
});
}
stopFrameSync() {
return new Promise((resolve, reject) => {
this.room.stopFrameSync({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("停止帧同步成功");
resolve(null);
} else {
reject(event.code);
}
});
});
}
sendToServer() {
const sendToGameServerPara = {
data: {
cmd: 1
}
};
this.room.sendToGameSvr(sendToGameServerPara, event => console.log(event));
}
sendFrame(data: any) {
this.room.sendFrame({ data }, err => {
if (err.code != 0) {
console.log("err", err)
}
});
}
onJoinRoom(event) {
if (this._eventListeners['playerJoin']) {
this._eventListeners['playerJoin'](event);
}
console.log("新玩家加入", event.data.joinPlayerId);
}
onLeaveRoom(event) {
if (this._eventListeners['playerLeave']) {
this._eventListeners['playerLeave'](event);
}
console.log("玩家退出", event.data.leavePlayerId);
}
onRecvFromClient() { }
onRecvFrame(event) {
if (this._eventListeners['frameEvent']) {
this._eventListeners['frameEvent'](event);
}
}
onStartFrameSync(event) {
if (this._eventListeners['gameStart']) {
this._eventListeners['gameStart'](event);
}
}
onStopFrameSync(event) { }
onRecvFromGameSvr() { }
}
\ No newline at end of file
{
"ver": "1.0.8",
"uuid": "4deb144e-da0b-4a45-9e42-7fbadb904f99",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment