我写了一个webSocketManager.js 里边封装了wesocket 基本内容,我理想中要的是不同场景的脚本可以共用这个webSocketManager.js 就是websocket 只需要建立一次连接,而不是每个场景脚本使用时都需要重新连接。
现在有个问题,我在websocket 的onmessage 中需要调用 当前场景脚本中自定义的方法messageGet。
我这个封装文件是从cocos 2d-js 中拿来的的里边 是这么调用的 cc.director.getScene().messageGet(evt);
但是 现在 在creator 中 报错 说 messageGet(evt) undefined! .大家是怎么解决这个问题呢? 欢迎指教!如果有封装好的webosocket 单例 类库, 欢迎分享,在此感谢
下面 是我的代码,从 cocos 2d-js 项目中找来的,还没改造好,很多问题,重点是 websocket 怎么和 场景脚本中方法联系起来。
var WEB_SOCKET_SWF_LOCATION = “/swf/WebSocketMain.swf”;
var WEB_SOCKET_DEBUG = true;
var ws_server = ‘ws://123.207.167.163:9010/ajaxchattest’;
var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket;
var WebSocketManager = cc.Class({
extends: cc.Component,
properties: {
_wsObj: null,
_wsReConnectTimes: 0,
_reConnectMax: 3,
_connectTimeout: 5,
_reConnectFlag: false,
_msg: null,
_msgKey: null,
_msgSendStatus: ‘nothing’,
_msgTimeout: 5,
_msgTimeoutTimes: 0,
_msgGet: ‘’,
_target: null,
_callback: null,
},
// 打开连接
//openConnect: function () {
ctor: function () {
if ( this ._wsObj){
this ._wsObj.close();
return ;
}
this ._wsObj = null ;
var self = this ;
this.host = ws_server;
this ._wsObj = new WebSocket(this.host);
//连接服务器
this ._wsObj.onopen = function (evt) {
self.openGet(evt);
};
//接收到消息
this ._wsObj.onmessage = function (evt) {
self.messageGet(evt);
};
//错误信息
this ._wsObj.onerror = function (evt) {
self.errorGet(evt);
};
this ._wsObj.onclose = function (evt) {
self.closeGet(evt);
};
},
// 连接超时判断
connectTimeoutCheck: function (){
if ( this ._wsObj && this ._wsObj.readyState == WebSocket.CONNECTING){
// 重连次数
if ( this ._wsReConnectTimes > this ._reConnectMax){
// 重试过多后,应该提示玩家目前网络不稳定
GY_ti_shi_popup.getInstance().show(L( 'gy:ws_wang_luo_bu_wen' ));
} else {
this ._wsReConnectTimes++;
GY_ti_shi_popup.getInstance().show(L( 'gy:ws_timeout' ), 'ws_connect_timeout' );
}
} else {
this .connectTimeoutHandle();
}
},
// 超时后 重新连接
connectTimeoutHandle: function (){
// 重新打开连接
this .closeConnect();
},
// 关闭连接
closeConnect: function () {
cc.log( "WS CLOSING." );
if ( this ._wsObj){
this ._wsObj.close();
}
},
// 连接后处理
openGet: function (evt) {
//连接后发送用户信息
//var login_data = '{"game_name":"car","type":"login"}';
// var login_data = '{"game_name":"car","type":"login","client_name":"'
// +wxNickname+'","open_ID":"'+wxOpneid+'","client_image":"'+wxHeadimgurl+'","game_id":"'+gameId+'","room_id":"'+roomId+'"}';
// //var login_data = '{"game_name":"car","type":"login","client_name":"'
// // +12124123+'","open_ID":"'+234234+'","client_image":"'+23+'","room_id":"10"}';
// this._wsObj.send(login_data);
},
// 获得消息
messageGet: function (evt) {
var m_data = eval("("+evt.data+")");
cc.log(evt.data);
//
// cc.log(cc.director.getScene().getName()); //waiting
// cc.log(cc.director.getScene().x); //0
// cc.log(cc.director.getScene().color); //object
// cc.log(cc.director.getScene().opacity); //255
// cc.log(cc.director.getScene().getComponents(cc.Sprite)); //255
// cc.log(cc.director.getScene().getChildByName('Canvas')); //waiting
// cc.log(cc.director.getScene().getChildByName('Canvas').getComponent(cc.Sprite)); //null
cc.director.getScene().messageGet(evt); //就是这句了,在creator 中 报messageGet未定义,实际上在我的场景脚本中确实是有这个方法的!!
cc.director.getRunningScene().messageGet(evt); //和上句一样,报messageGet未定义
switch(m_data['type']){
// 服务端ping客户端
case 'ping':
//cc.director.getRunningScene().MessageGet(evt);
//this.sendGet('{"game_name":"car","type":"pong"}');
break;
//
case 'login':
break;
//
case 'say':
break;
//
case 'logout':
break;
}
},
// 获取错误
errorGet: function (evt) {
cc.log( "WS Error" );
this .closeConnect();
},
// 连接关闭处理
closeGet: function (evt) {
cc.log( "websocket instance closed." );
this ._wsObj = null ;
// 连接关闭马上进行重试
this .openConnect();
},
/**
* 给服务器发送消息
* @param act
* @param params
* @param callback
* @param target
*/
//发送数据
sendGet: function (str) {
// 判断当前连接
if ( this .isOpen()) {
this .sendRequest(str);
}
else {
this .openConnect();
}
},
// 发送消息
sendRequest: function (str) {
this ._wsObj.send(str);
// this ._msgSendStatus = 'msgSend' ;
// 设置超时时间
//director.getScheduler().scheduleCallbackForTarget( this , this .sendTimeoutCheck, 0 , 0 , this ._msgTimeout);
},
// 消息被响应了
requestResponse: function (resObj) {
// 获得响应的消息后,去掉 loading 遮罩
director.getScheduler().unscheduleCallbackForTarget( this , this .sendTimeoutCheck);
this ._msg = null ;
this ._msgSendStatus = 'nothing' ;
GY_loading_popup.getInstance().hide();
this ._callback.call( this ._target, resObj._body);
},
// 发送消息超时判断
sendTimeoutCheck: function (){
if ( this ._msgSendStatus == 'msgSend' ){
// 消息没有被响应,去掉 loading 遮罩
GY_loading_popup.getInstance().hide();
GY_ti_shi_popup.getInstance().show(L( 'gy:ws_timeout' ), 'ws_timeout' );
}
},
// 超时后
sendTimeoutHandle: function (){
var act = 'gc/jiao_se/deng_lu' ;
var param = {};
param.gc_token = LS.getItem( 'gc_token' );
param.id_yong_hu = LS.getItem( 'id_yong_hu' );
param.zhouqi = LS.getItem( 'uc_zhouqi' );
HttpManager.create().sendGetRequest(act, param, this .callbackTimeoutHandle, this );
},
// 超时消息处理
callbackTimeoutHandle: function (resObj){
if (resObj.st_code == 1 ) {
if (G.js_jiao_se.zt_jiao_se == 1 ) {
SM.goto_page( 'DL_chuang_ming_page' );
} else if (G.js_jiao_se.zt_jiao_se == 2 ) {
SM.goto_page( 'YD_yin_dao_' + G.js_jiao_se.xin_shou_jin_du + '_page' );
} else if (G.js_jiao_se.zt_jiao_se == 3 ) {
//SM.goto_page('SY_shou_ye_page');
// 试试看直接刷新页面的效果吧
SM.flush_page();
}
}
},
// 判断 ws 是否已经连接
isOpen: function (){
return ( this ._wsObj && this ._wsObj.readyState == WebSocket.OPEN);
},
purge: function () {
NC.removeObserver( this , 'ws_timeout' );
NC.removeObserver( this , 'ws_connect_timeout' );
this .closeConnect();
this ._instance = null ;
}
});
// WebSocketManager._instance = null ;
//
// WebSocketManager.getInstance = function () {
// if (! this ._instance) {
// this ._instance = new WebSocketManager();
// }
// return this ._instance;
//
// };
//module.exports = WebSocketManager;
var obj = new WebSocketManager();
module.exports = obj;