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

feat: 替换华为云联机服务

parent a9f795ff
{"ver":"1.1.2","uuid":"c35bb2f6-f24a-4850-ae44-643f2fdc7541","isBundle":false,"bundleName":"","priority":1,"compressionType":{},"optimizeHotUpdate":{},"inlineSpriteFrames":{},"isRemoteBundle":{"ios":false,"android":false},"subMetas":{}} {
\ No newline at end of file "ver": "1.1.2",
"uuid": "c35bb2f6-f24a-4850-ae44-643f2fdc7541",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {
"ios": false,
"android": false
},
"subMetas": {}
}
\ No newline at end of file
...@@ -30,7 +30,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -30,7 +30,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this._audioResList.push({ url: this.data.waitingTurn }); this._audioResList.push({ url: this.data.waitingTurn });
} }
addPreloadAnima() {} addPreloadAnima() { }
onLoadEnd() { onLoadEnd() {
// TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考 // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
...@@ -54,17 +54,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -54,17 +54,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
async initData() { async initData() {
this.networkHelper = new NetworkHelper(); this.networkHelper = new NetworkHelper();
this.initNetworkListener(); this.initNetworkListener();
do {
this.playerId = await this.networkHelper.init("op_16", 2); this.playerId = await this.networkHelper.init("op_16", 2);
if (this.playerId === null) {
console.log("onDestroy");
this.networkHelper.onDestroy();
}
console.log("this.playerId = " + this.playerId);
} while (this.playerId === null);
const room = this.networkHelper.room; const room = this.networkHelper.room;
if (this.playerId == room.roomInfo.owner) { if (this.playerId == room.ownerId) {
console.log("房主"); console.log("房主");
this.isRoomOwner = true; this.isRoomOwner = true;
this.server = new GameServer(2, this.networkHelper); this.server = new GameServer(2, this.networkHelper);
...@@ -642,9 +636,16 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -642,9 +636,16 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.networkHelper.leaveRoom(); this.networkHelper.leaveRoom();
this.log("playerOffLine" + JSON.stringify(event)); this.log("playerOffLine" + JSON.stringify(event));
}); });
this.networkHelper.on("gameStart", (event) => {}); this.networkHelper.on("gameStart", (event) => { });
this.networkHelper.on("frameEvent", (event) => { this.networkHelper.on("frameEvent", (frameData) => {
event.data.frame.items.forEach(async (item) => { const dataList = [];
if (frameData instanceof Array) {
dataList.push(...frameData);
} else {
dataList.push(frameData);
}
dataList.forEach(async (itemStr) => {
const item = JSON.parse(itemStr);
if (this.server) { if (this.server) {
this.server.onFrameEvent(item.data); this.server.onFrameEvent(item.data);
} }
......
import { Base64 } from "js-base64";
export class NetworkHelper { export class NetworkHelper {
_eventListeners: any = {}; _eventListeners: any = {};
ctor() { } ctor() { }
...@@ -10,10 +8,11 @@ export class NetworkHelper { ...@@ -10,10 +8,11 @@ export class NetworkHelper {
async init(roomType: string, maxPlayers: number) { async init(roomType: string, maxPlayers: number) {
try { try {
this.initRoom(); await this.initRoom();
await this.initListener(); // await this.initListener();
return await this.joinRoom(roomType, maxPlayers); return await this.joinRoom(roomType, maxPlayers);
} catch (e) { } catch (e) {
console.log(e);
// return null; // return null;
} }
} }
...@@ -29,10 +28,6 @@ export class NetworkHelper { ...@@ -29,10 +28,6 @@ export class NetworkHelper {
await this.leaveRoom(); await this.leaveRoom();
} }
getRoomInfo() {
return this.room.roomInfo;
}
listenerInited = false; listenerInited = false;
initListener() { initListener() {
...@@ -111,162 +106,94 @@ export class NetworkHelper { ...@@ -111,162 +106,94 @@ export class NetworkHelper {
}); });
} }
room: MGOBE.Room; room: GOBE.Room;
initRoom() { client: GOBE.Client;
this.room = new MGOBE.Room(); async initRoom() {
MGOBE.Listener.add(this.room); this.client = new window.GOBE.Client({
this.room.onJoinRoom = this.onJoinRoom.bind(this); clientId: '857970243220294656',// 客户端ID
this.room.onLeaveRoom = this.onLeaveRoom.bind(this); clientSecret: '489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D',// 客户端密钥
this.room.onRecvFromClient = this.onRecvFromClient.bind(this); appId: '243650030995634151',// 应用的ID
this.room.onChangePlayerNetworkState = openId: 'op_16' + Math.random(),// 玩家ID
this.onChangePlayerNetworkState.bind(this); });
this.room.onRecvFrame = this.onRecvFrame.bind(this); console.log(this.client);
this.room.onStartFrameSync = this.onStartFrameSync.bind(this); console.log('喵喵喵1');
this.room.onStopFrameSync = this.onStopFrameSync.bind(this); let playerId;
this.room.onRecvFromGameSvr = this.onRecvFromGameSvr.bind(this); this.client.init().then(() => {
console.log("this.room = " + JSON.stringify(this.room.roomInfo)); // 初始化成功
playerId = this.client.playerId;
}).catch((e) => {
// 初始化失败
console.log('初始化失败')
console.log(e)
});
console.log('喵喵喵2');
} }
player: any; player: any;
joinRoom(roomType: string, maxPlayers: number, customData: string = "") { async joinRoom(roomType: string, maxPlayers: number) {
return new Promise((resolve, reject) => { const matchRoomConfig = {
const playerName = "Tom" + Math.random(); matchParams: {
const playerInfo = { matchRule: '',
name: playerName, matchRule2: '',
customPlayerStatus: 0, },
customProfile: customData, roomType: roomType,
}; customRoomProperties: '',
maxPlayers: maxPlayers,
const matchRoomPara = { };
playerInfo, const playerConfig = {
maxPlayers: maxPlayers, customPlayerStatus: 0,
roomType: roomType, customPlayerProperties: ''
}; };
console.log('汪汪汪1')
this.room.matchRoom(matchRoomPara, async (event) => { this.room = await this.client.matchRoom(matchRoomConfig, playerConfig);
// console.log(JSON.stringify(event)) this.player = this.room.player;
if (event.code !== 0) {
this.log("匹配失败"); this.room.onJoin(this.onJoinRoom.bind(this));
reject(event); this.room.onLeave(this.onLeaveRoom.bind(this));
return; this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
} this.room.onStopFrameSync(this.onStopFrameSync.bind(this));
this.log("匹配成功: " + event.data.roomInfo.id); this.room.onRecvFrame(this.onRecvFrame.bind(this));
const player = event.data.roomInfo.playerList.find( this.room.onDisconnect(this.onDisconnect.bind(this));
(player) => player.name == playerName this.room.onDismiss(this.onDisconnect.bind(this));
); console.log('汪汪汪2')
this.player = player;
if (this.checkRoomOwnerOffLine()) {
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
this.log("房主掉线了");
resolve(null);
return;
}
resolve(player.id);
});
});
} }
leaveRoom() { async leaveRoom() {
return new Promise((resolve, reject) => { await this.room.leave();
this.room.leaveRoom({}, (event) => { console.log("退房成功");
console.log(JSON.stringify(event));
if (event.code === 0) {
console.log("退房成功" + this.room.roomInfo.id);
// this.room.initRoom();
resolve(null);
} else {
console.log("退房失败");
}
});
});
} }
dismissRoom() { async dismissRoom() {
const roomInfo = this.room.roomInfo; if (this.room.ownerId == this.player.id) {
const player = roomInfo.playerList.find( await this.client.dismissRoom();
(player) => player.name == this.player.name console.log("房间已解散");
);
if (roomInfo.owner == player.id) {
return new Promise((resolve, reject) => {
this.room.dismissRoom({}, (res) => {
console.log("房间已解散");
});
});
} }
} }
closeRoom() { closeRoom() {
return new Promise((resolve, reject) => { return;
this.room.changeRoom({ isForbidJoin: true }, (event) => { // return new Promise((resolve, reject) => {
console.log("关门"); // this.room.changeRoom({ isForbidJoin: true }, (event) => {
// console.log(JSON.stringify(event)) // console.log("关门");
resolve(null); // // console.log(JSON.stringify(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(JSON.stringify(event));
if (event.code === 0) {
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
resolve(null);
} else {
reject();
}
});
});
} }
stopFrameSync() { async startFrameSync() {
return new Promise((resolve, reject) => { await this.room.startFrameSync();
if (!this.room) { cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
resolve(null);
return;
}
this.room.stopFrameSync({}, (event) => {
console.log(JSON.stringify(event));
if (event.code === 0) {
console.log("停止帧同步成功");
resolve(null);
} else {
console.log("停止帧同步失败");
reject(event.code);
}
});
});
} }
sendToServer() { async stopFrameSync() {
const sendToGameServerPara = { await this.room.stopFrameSync();
data: { console.log("停止帧同步成功");
cmd: 1,
},
};
this.room.sendToGameSvr(sendToGameServerPara, (event) =>
console.log(event)
);
} }
sendFrame(data: any) { sendFrame(data: any) {
console.log('sendFrame: ' + JSON.stringify(data)); console.log('sendFrame: ' + JSON.stringify(data));
this.room.sendFrame({ data }, (err) => { this.room.sendFrame(JSON.stringify(data));
if (err.code != 0) {
console.log("err", err);
}
});
} }
onJoinRoom(event) { onJoinRoom(event) {
...@@ -284,22 +211,10 @@ export class NetworkHelper { ...@@ -284,22 +211,10 @@ export class NetworkHelper {
} }
onRecvFromClient() { } onRecvFromClient() { }
checkRoomOwnerOffLine() { onDisconnect(event) {
return this.room.roomInfo.playerList.some(player => { this.log("玩家掉线了: " + JSON.stringify(event));
return ( if (this._eventListeners["playerOffLine"]) {
player.commonNetworkState == MGOBE.types.NetworkState.COMMON_OFFLINE || this._eventListeners["playerOffLine"](event);
player.relayNetworkState == MGOBE.types.NetworkState.RELAY_OFFLINE
);
})
}
onChangePlayerNetworkState(event) {
this.log("玩家网络变化: " + JSON.stringify(event));
if (this.checkRoomOwnerOffLine()) {
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
this.log("房主掉线了");
} }
} }
......
{
"ver": "1.1.2",
"uuid": "4e259305-46b1-43d9-8d93-25fea822e0d9",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {},
"subMetas": {}
}
\ No newline at end of file
This diff is collapsed.
{
"ver": "2.0.0",
"uuid": "de16aac1-0c28-4863-91c6-20105020daae",
"subMetas": {}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"ver": "1.0.8",
"uuid": "291a4bb1-3d0d-41ce-9506-c5808b1d2081",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
{
"type": "software",
"scope": "game",
"name": "GameOBESDK",
"version": "1.1.4.300",
"playbook": ""
}
\ No newline at end of file
{
"ver": "1.0.0",
"uuid": "f7dd520f-d3cf-4b95-b0c3-9f3a7ee3d7c6",
"subMetas": {}
}
\ No newline at end of file
{
"ver": "1.0.8",
"uuid": "497a857a-a098-429a-97b0-c1a6ae0b2a16",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
...@@ -345,12 +345,12 @@ export function showTrebleFirework(baseNode, rabbonList) { ...@@ -345,12 +345,12 @@ export function showTrebleFirework(baseNode, rabbonList) {
showFireworks(right); showFireworks(right);
} }
export function onHomeworkFinish() { export function onHomeworkFinish(data: any = null) {
const middleLayer = cc.find('middleLayer'); const middleLayer = cc.find('middleLayer');
if (middleLayer) { if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer'); const middleLayerComponent = middleLayer.getComponent('middleLayer');
if (middleLayerComponent.role == 'student') { if (middleLayerComponent.role == 'student') {
middleLayerComponent.onHomeworkFinish(() => { }); middleLayerComponent.onHomeworkFinish(() => { }, data);
} }
} else { } else {
console.log('onHomeworkFinish'); console.log('onHomeworkFinish');
......
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