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

feat:

parent 860b4b42
import { RandomInt } from "./util";
export class NetworkHelper {
_eventListeners: any = {};
ctor() { }
......@@ -27,79 +29,51 @@ export class NetworkHelper {
room: any;
client: any;
async initRoom() {
this.client = new (<any>(window)).GOBE.Client({
clientId: '857970243220294656',// 客户端ID
clientSecret: '489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D',// 客户端密钥
appId: '105879509',// 应用的ID
openId: 'op_16' + Math.random(),// 玩家ID
});
await this.client.init();
this.client = {};
}
player: any;
async joinRoom(roomType: string, maxPlayers: number) {
const matchRoomConfig = {
matchParams: {
param1: 'param1',
},
roomType: roomType,
maxPlayers: maxPlayers,
};
this.room = await this.client.matchRoom(matchRoomConfig);
const playerId = RandomInt(10000000);
this.room = new FakeRoom(playerId);
this.room.players.push(this.room.player);
this.player = this.room.player;
console.log('汪汪汪: ' + this.room.players.length);
await this.startFrameSync();
console.log('汪汪汪2');
this.room.onJoin(this.onJoinRoom.bind(this));
this.room.onLeave(this.onLeaveRoom.bind(this));
// this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this.room.onStopFrameSync(this.onStopFrameSync.bind(this));
this.room.onRecvFrame(this.onRecvFrame.bind(this));
this.room.onDisconnect(this.onDisconnect.bind(this));
this.room.onDismiss(this.onDisconnect.bind(this));
await this.startFrameSync();
return this.player.playerId;
}
async leaveRoom() {
await this.room.leave();
console.log("退房成功");
}
async dismissRoom() {
if (this.room.ownerId == this.player.id) {
await this.client.dismissRoom();
console.log("房间已解散");
}
}
async closeRoom() {
await this.client.cancelMatch();
}
async startFrameSync() {
await this.room.startFrameSync();
// return new Promise((resolve, reject) => {
// this.room.onStartFrameSync(() => {
// cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
// resolve(null);
// });
// });
console.log("开始帧同步成功");
}
async stopFrameSync() {
await this.room.stopFrameSync();
console.log("停止帧同步成功");
}
sendFrame(data: any) {
console.warn('sendFrame: ' + JSON.stringify(data));
if (this.room._isSyncing) {
this.room.sendFrame(JSON.stringify(data));
} else {
console.log('is syncing = false');
this.onRecvFrame({data});
}
this.onRecvFrame({ data });
}
onJoinRoom(event) {
......@@ -196,3 +170,41 @@ export function asyncDelay(time) {
.start();
});
}
class FakeRoom {
ownerId;
players = [];
player: any;
constructor(playerId) {
this.player = { playerId: playerId };
this.ownerId = playerId;
}
_onJoin;
onJoin(func: Function) {
this._onJoin = func;
}
_onLeave;
onLeave(func: Function) {
this._onLeave = func;
}
_onStartFrameSync;
onStartFrameSync(func: Function) {
this._onStartFrameSync = func;
}
_onStopFrameSync;
onStopFrameSync(func: Function) {
this._onStopFrameSync = func;
}
_onRecvFrame;
onRecvFrame(func: Function) {
this._onRecvFrame = func;
}
_onDisconnect;
onDisconnect(func: Function) {
this._onDisconnect = func;
}
_onDismiss;
onDismiss(func: Function) {
this._onDismiss = func;
}
}
\ No newline at end of file
export class NetworkHelper {
_eventListeners: any = {};
ctor() { }
on(eventName, func) {
this._eventListeners[eventName] = func;
}
async init(roomType: string, maxPlayers: number) {
await this.initRoom();
return await this.joinRoom(roomType, maxPlayers);
}
async startGame() {
await this.startFrameSync();
await this.closeRoom();
}
async stopGame() {
await this.stopFrameSync();
await this.closeRoom();
await this.leaveRoom();
}
listenerInited = false;
room: any;
client: any;
async initRoom() {
this.client = new (<any>(window)).GOBE.Client({
clientId: '857970243220294656',// 客户端ID
clientSecret: '489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D',// 客户端密钥
appId: '105879509',// 应用的ID
openId: 'op_16' + Math.random(),// 玩家ID
});
await this.client.init();
}
player: any;
async joinRoom(roomType: string, maxPlayers: number) {
const matchRoomConfig = {
matchParams: {
param1: 'param1',
},
roomType: roomType,
maxPlayers: maxPlayers,
};
this.room = await this.client.matchRoom(matchRoomConfig);
this.player = this.room.player;
console.log('汪汪汪: ' + this.room.players.length);
await this.startFrameSync();
console.log('汪汪汪2');
this.room.onJoin(this.onJoinRoom.bind(this));
this.room.onLeave(this.onLeaveRoom.bind(this));
// this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this.room.onStopFrameSync(this.onStopFrameSync.bind(this));
this.room.onRecvFrame(this.onRecvFrame.bind(this));
this.room.onDisconnect(this.onDisconnect.bind(this));
this.room.onDismiss(this.onDisconnect.bind(this));
return this.player.playerId;
}
async leaveRoom() {
await this.room.leave();
console.log("退房成功");
}
async dismissRoom() {
if (this.room.ownerId == this.player.id) {
await this.client.dismissRoom();
console.log("房间已解散");
}
}
async closeRoom() {
await this.client.cancelMatch();
}
async startFrameSync() {
await this.room.startFrameSync();
// return new Promise((resolve, reject) => {
// this.room.onStartFrameSync(() => {
// cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
// resolve(null);
// });
// });
}
async stopFrameSync() {
await this.room.stopFrameSync();
console.log("停止帧同步成功");
}
sendFrame(data: any) {
console.warn('sendFrame: ' + JSON.stringify(data));
if (this.room._isSyncing) {
this.room.sendFrame(JSON.stringify(data));
} else {
console.log('is syncing = false');
this.onRecvFrame({data});
}
}
onJoinRoom(event) {
if (this._eventListeners["playerJoin"]) {
this._eventListeners["playerJoin"](event);
}
console.log("新玩家加入", JSON.stringify(event));
this.room.players.forEach(player => {
console.log('player.playerId = ', player.playerId);
});
}
onLeaveRoom(event) {
console.log("onLeaveRoom");
if (this._eventListeners["playerLeave"]) {
this._eventListeners["playerLeave"](event);
}
console.log("玩家退出", event.data.leavePlayerId);
}
onRecvFromClient() { }
onDisconnect(event) {
this.log("玩家掉线了: " + JSON.stringify(event));
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
}
onRecvFrame(event) {
if (this._eventListeners["frameEvent"]) {
this._eventListeners["frameEvent"](event);
}
}
onStartFrameSync(event) {
console.log('onStartFrameSync');
if (this._eventListeners["gameStart"]) {
this._eventListeners["gameStart"](event);
}
}
onStopFrameSync(event) { }
onRecvFromGameSvr() { }
async onDestroy() {
try {
console.log("onDestroy1");
// MGOBE.Listener.clear();
this.stopFrameSync();
this.closeRoom();
this.dismissRoom();
this.leaveRoom();
console.log("onDestroy2");
} catch (e) {
console.log(JSON.stringify(e));
}
}
log(str) {
const node = cc.find("middleLayer");
if (node) {
node.getComponent("middleLayer").log(str);
} else {
cc.log(str);
}
}
callNetworkApiGet(baseUrl, uri, data, callBack) {
let queryStr = "?";
const params = [];
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
params.push(`${key}=${data[key]}`);
}
}
queryStr += params.join("&");
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && xhr.status >= 200 && xhr.status < 400) {
callBack(xhr.responseText);
}
};
const url = `${baseUrl}${uri}${queryStr}`;
console.log("url = " + url);
xhr.open("GET", url, true);
xhr.send();
}
}
export function asyncDelay(time) {
return new Promise((resolve, reject) => {
cc.tween(cc.find("Canvas"))
.delay(time)
.call(() => {
resolve(null);
})
.start();
});
}
{
"ver": "1.0.8",
"uuid": "e3b1625a-4a5d-493a-a856-9cce9f53169a",
"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