加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

quick-cocos2dx中使用pomelo

发布时间:2020-12-14 20:34:34 所属栏目:百科 来源:网络整理
导读:原帖地址:http://www.jb51.cc/article/p-mnvjfajx-bce.html 这里博客教程使用pomelo中的聊天例子进行通信 在git上已经有大神开源了quick和pomelo通信的代码https://github.com/luoxinliang/pomelo_quick_x 。 但是我在使用中发现会有一些问题 ,如客户端不

原帖地址: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会把回调函数销毁,所以才接收不到服务器端发送的消息。然后把代码改成下面的就可以拉

[plain] view plain copy print ?
  1. functionM:onLoginClick()
  2. self:queryEntry(function(host,port)
  3. game.pomelo:init({host=host,port=port},
  4. function()
  5. localroute="connector.entryHandler.enter"
  6. game.pomelo:request(route,{username=self.username,rid=self.rid},
  7. function(data)
  8. ifdata.errorthen
  9. print("loginfail!error=%s",data.error)
  10. else
  11. print("loginsuccess!")
  12. game.pomelo:on("onChat",handler(self,self.onChat))
  13. game.pomelo:on("onAdd",self.onAdd))
  14. game.pomelo:on("onLeave",self.onLeave))
  15. end
  16. end
  17. )
  18. end)
  19. end)
  20. end


就是在对connector连接成功的时候加上回调函数,这样就可以接收到服务器发送的信息拉。

接下来解决中文乱码问题

下面代码是网友 skyblue提供 代码

[plain] view plain copy print ?
  1. Protocol.strencode=function(str)
  2. --table.packAll(string.byte(str,1,#str))
  3. doreturn{string.byte(str,#str)}end
  4. end
  5. Protocol.strdecode=function(bytes)
  6. --dump(bytes)
  7. localarray={}
  8. locallen=#bytes
  9. fori=1,lendo
  10. --table.insert(array,string.char(bytes[i]))
  11. array[i]=string.char(bytes[i])--更快一些
  12. end
  13. --dump(array)
  14. returntable.concat(array)
  15. end

把上面的代码替换掉原来的Protocol中的方法就可以解决中文乱码问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读