客户端
开始场景:menuScript.js
import global from './global';
var com = require('./common');
cc.Class({
extends: cc.Component,
properties: {
backAudio: {
default: null,
type:cc.AudioClip
},
readyLab: {
default: null,
type:cc.Label
}
},
onLoad: function () {
// cc.audioEngine.playEffect(this.backAudio, true);
//监听服务器下发开始游戏
global.socket.onCanPlayChess(function () {
com.gameMode = 'rd' //游戏模式转换
cc.director.loadScene('gameScene'); //加载游戏场景
});
},
onButtonClick(event,optData) {
console.log('optData=' + optData);
switch (optData) {
case 'random'://随机匹配玩家
//显示正在匹配中UI
this.readyLab.node.active = true;
//发送随机匹配请求
global.socket.reqRandomStart({
accountID: global.playerData.accountid,
nickName: global.playerData.nickName
}, (err, data)=>{
if (err) {//失败
console.log('失败');
} else {
console.log(console.log(JSON.stringify(data)));
//根据data判断下子的颜色
if (data.data.chessColor === 'b') {
com.chessColor = 'b';
} else { //是白球开始下棋关闭
com.chessColor = 'w';
com.gameState='close'
}
}
});
break;
case 'af': //与人工智障对决
com.gameMode = 'af';
cc.director.loadScene('gameScene');
break;
default: break;
}
}
});
socket封装
import eventListener from './../listener/eventListener'
const scoketController = function () {
let that = {};
let _socket = io(defines.serverUrl);
let _callBackMap = {};
let _callBackIndex = 1;
let _event=eventListener({});
//监听服务器下发消息
_socket.on('notify', function (data) {
console.log('-----------监听到服务器下发的消息');
//-------------回调部分------------
//获取回调函数id
let callBackIndex = data.callBackIndex;
//执行序号为id的函数
if (_callBackMap.hasOwnProperty(callBackIndex)) {
let cb = _callBackMap[callBackIndex];
if (data.data.err) {
cb(data.data.err);
} else {
cb(null, data);
}
}
//-------------主动监听服务器下发的消息----
let type=data.type;
_event.fire(type,data.data);
});
//向服务器发送通知
const notify = function (type, data, callBackIndex) {
_socket.emit('notify', {type: type, data: data, callBackIndex: callBackIndex });
console.log('3');
};
//向服务器发送请求
const request = function (type, data, cb) {
console.log('2');
_callBackIndex++;
_callBackMap[_callBackIndex] = cb;
notify(type, data, _callBackIndex);
};
//请求随机匹配开始游戏
that.reqRandomStart=function(data,cb){
console.log('1');
request('random',data,cb);
};
//请求上传棋子
that.reqUpChess=function(data,cb){
request('upChess',data,cb);
};
//监听服务器下发开始游戏
that.onCanPlayChess=function(cb){
_event.on('start_game',cb);
};
//监听玩家下的棋子
that.onPlayChess=function(cb){
_event.on('playChess',cb);
};
return that;
};
export default scoketController;
playerData
const getRandomStr = function (count) {
let str = '';
for (let i = 0; i < count; i++) {
str += Math.floor(Math.random() * 10);
}
return str;
};
const playerData = function () {
let that = {};
that.id = '1' + getRandomStr(6); //用户id
that.accountid = '2' + getRandomStr(6);//用户账号
that.nickName = 'xiaoming' + getRandomStr(2);//用户昵称
that.avatar_url = 'http://ent.qingdaonews.com/images/2019-03/01/1575ec9a-81c4-4add-8e0f-832ac8cde9ad.jpg';
that.house_card_count = getRandomStr(6);
return that;
};
export default playerData;
监听器istener\eventListener.js
const eventListener = function (obj) {
let Register = {};
obj.on = function (type, method) {
if (Register.hasOwnProperty(type)) {
Register[type].push(method);
} else {
Register[type] = [method];
}
};
obj.fire = function (type) {
if (Register.hasOwnProperty(type)) {
let hanlderList = Register[type];//获取函数列表
for (let i = 0; i < hanlderList.length; i++) {
let handler = hanlderList[i];
let args = [];
for (let j = 1; j < arguments.length; j++) {
args.push(arguments[j]);
}
handler.apply(this, args);
}
}
};
obj.removeListener = function (type) {
Register[type] = [];
};
obj.removeAllListener = function (type) {
Register = {};
}
return obj;
};
export default eventListener;
公共变量:common.js
module.exports = {
gameMode: 'af', //游戏模式
chessColor: 'b', //玩家棋类型
gameState:'start' //下棋开启与关闭
}
宏定义define.js
const defines = {};
defines.serverUrl = 'http://localhost:8888'
window.defines = defines;
全局变量global.js:
import socketController from './data/socket_controller'
import playerData from './data/playerData'
import eventListener from './listener/eventListener'
const global={};
global.socket=socketController();
global.playerData=playerData();
global.eventListener=eventListener({});
export default global;