quick-cocos2dx中使用pomelo
原帖地址:http://www.52php.cn/article/p-mnvjfajx-bce.html 这里博客教程使用pomelo中的聊天例子进行通信 在git上已经有大神开源了quick和pomelo通信的代码https://github.com/luoxinliang/pomelo_quick_x 。 但是我在使用中发现会有一些问题 ,如客户端不能接收到服务器发的聊天信息和中文乱码。 以下的修改都是基于上面git地址上的代码 首先解决客户端不能够接收服务器端聊天的信息的问题 quick发送连接请求之后会向pomelo的gate服务器发送消息gate服务器把connector服务器的ip地址和端口返回给客户端,用quick连接上connector服务器之后可以向服务器端发送消息但是就是接收不到消息 。通过查看代码发现是Emitter文件中emit函数中self._callbacks为空没有回调函数 。可是明明在场景初始化的时候给他加上了回调函数 。纠结了半天原来是因为回调函数初始化是在连接gate服务器的时候设置的,在连接上gate服务器返回connector服务器信息的时候断开了连接,这个时候pomelo会把回调函数销毁,所以才接收不到服务器端发送的消息。然后把代码改成下面的就可以拉
function M:onLoginClick() self:queryEntry(function(host,port) game.pomelo:init({host=host,function() local route = "connector.entryHandler.enter" game.pomelo:request(route,function(data) if data.error then print("login fail! error=%s",data.error) else print("login success!") game.pomelo:on("onChat",self.onChat)) game.pomelo:on("onAdd",self.onAdd)) game.pomelo:on("onLeave",self.onLeave)) end end ) end) end) end
接下来解决中文乱码问题 下面代码是网友 skyblue提供 代码
Protocol.strencode = function (str) --table.packAll(string.byte(str,#str)) do return {string.byte(str,#str)} end end Protocol.strdecode = function(bytes) -- dump(bytes) local array = {} local len = #bytes for i = 1,len do -- table.insert(array,string.char(bytes[i])) array[i] = string.char(bytes[i]) -- 更快一些 end -- dump(array) return table.concat(array) end 把上面的代码替换掉原来的Protocol中的方法就可以解决中文乱码问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |