Coder Social home page Coder Social logo

pomelococoscreator's Introduction

pomelococoscreator

专为cocos creator修改的pomelo游戏框架的客户端js代码,使用的是websocket协议,服务端初始化时,通讯类型选第一个(ws)

源文件列表及地址

修改内容

将上面的文件直接扔进工程,是无法直接使用pomelo,需要做一些简单修改,修改后的文件放在 pomelo这里。将代码丢进工程, 再require("relative_path/pomelo-client")即可。

连接到服务器

pomelo.init(params, callback);

例子:

pomelo.init({
	host: host,
	port: port,
	user: {},
	handshakeCallback : function(){}
}, function() {
	console.log('success');
});
//@param user 发送给服务器的json串
//@param handshakeCallback 握手回调

发送request到服务器

pomelo.request(route, msg, callback);

例子:

pomelo.request(route, {
    rid: rid
}, function(data) {
	console.log(dta);   
});

发送request到服务器没有回调,即通知(notify)。

pomelo.notify(route, params);

从服务器接收消息,即推送(push)。

pomelo.on(route, callback);

例子:

pomelo.on('onChat', function(data) {
    addMessage(data.from, data.target, data.msg);
    $("#chatHistory").show();
});

断开连接

pomelo.disconnect();

普通事件

pomelo.emit('beforeRPC'); //调用rpc之前
pomelo.emit("afterRPC"); //调用rpc之后

适用于网络请求过程中添加遮罩

设置回调过滤函数

适用于处理服务器返回错误信息

pomelo.setFilterAfter = function(cb){
   filterAfter = cb;
}

异常

以下内容为查看源码所得,不保证正确性

异常触发的事件

pomelo.emit('reconnect');
pomelo.emit('io-error', event);
pomelo.emit('close',event);
pomelo.emit('disconnect', event);
pomelo.emit('heartbeat timeout');
pomelo.emit('error', 'client version not fullfill');
pomelo.emit('error', 'handshake fail');
pomelo.emit('onKick', data);

设置异常事件处理(事件名对应即可)

pomelo.on("reconnect", callback);

pomelo.on("close",  function(event){
	
});

pomelo.on("io-error", function(event){

});

异常事件状态码

由于close事件可以是正常关闭,也有可能是异常关闭,所以需要根据关闭时,事件的code进行相应的处理 状态码查询地址: MDN

需要注意的是,socket的error事件对应pomelo的事件是io-error。

注意事项

  1. 使用pomelo.on监听事件的时候,如果对同一事件重复监听,所有的监听事件都会被调用。所以,使用pomelo.on事件前,最好先清理一下。

    pomelo.removeAllListeners([event])

  2. protobuf.js,protocol.js在creator里,有些代码是冗余的,但为了保持与官方的一致,并未进行精简。

pomelococoscreator's People

Contributors

isghost avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pomelococoscreator's Issues

cocos creator 运行错误

我将ccc-pomelo-chat-client-master解压缩,然后下载pomelococoscreator-master 解压缩后,放入assets/pomelo然后运行出现下面错误,我的运行方式有错吗?

TypeError: Cannot set property 'protobuf' of undefined
at Object.require.protobuf: "})(typeof(window) == "undefined" ? module.exports : (this.protobuf = {}), this)" (assets/pomelo/protobuf.js:37:1)
at s (file:///Users/andy.chiang/workingArea/cocos2dx/ccc-pomelo-chat-client-master/library/bundle.project.js?001:1:262)
at file:///Users/andy.chiang/workingArea/cocos2dx/ccc-pomelo-chat-client-master/library/bundle.project.js?001:1:313
at Object.require.pomelo-client.events (assets/pomelo/pomelo-client.js:1)
at s (file:///Users/andy.chiang/workingArea/cocos2dx/ccc-pomelo-chat-client-master/library/bundle.project.js?001:1:262)
at e (file:///Users/andy.chiang/workingArea/cocos2dx/ccc-pomelo-chat-client-master/library/bundle.project.js?001:1:433)
at file:///Users/andy.chiang/workingArea/cocos2dx/ccc-pomelo-chat-client-master/library/bundle.project.js?001:1:451

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.