quick-cocos2dx中使用pomelo

原帖地址:http://blog.csdn.net/justforthedream/article/details/39030853

 

 

这里博客教程使用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会把回调函数销毁,所以才接收不到服务器端发送的消息。然后把代码改成下面的就可以拉

  1. function M:onLoginClick()  
  2.     self:queryEntry(function(host,port)  
  3.         game.pomelo:init({host=host,port=port},  
  4.             function()  
  5.                 local route = "connector.entryHandler.enter"  
  6.                 game.pomelo:request(route,{username=self.username,rid=self.rid},  
  7.                     function(data)  
  8.                         if data.error then  
  9.                             print("login fail! error=%s",data.error)  
  10.                         else  
  11.                             print("login success!")  
  12.                             game.pomelo:on("onChat",handler(self,self.onChat))  
  13.                             game.pomelo:on("onAdd",handler(self,self.onAdd))  
  14.                             game.pomelo:on("onLeave",handler(self,self.onLeave))  
  15.                         end  
  16.                     end  
  17.                 )  
  18.             end)  
  19.     end)  
  20. end  
function M:onLoginClick()
    self:queryEntry(function(host,port)
        game.pomelo:init({host=host,port=port},
            function()
                local route = "connector.entryHandler.enter"
                game.pomelo:request(route,{username=self.username,rid=self.rid},
                    function(data)
                        if data.error then
                            print("login fail! error=%s",data.error)
                        else
                            print("login success!")
                            game.pomelo:on("onChat",handler(self,self.onChat))
                            game.pomelo:on("onAdd",handler(self,self.onAdd))
                            game.pomelo:on("onLeave",handler(self,self.onLeave))
                        end
                    end
                )
            end)
    end)
end


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

接下来解决中文乱码问题 

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

  1. Protocol.strencode = function (str)  
  2.     --table.packAll(string.byte(str,1,#str))  
  3.     do return {string.byte(str,1,#str)} end  
  4. end  
  5.   
  6. Protocol.strdecode = function(bytes)  
  7.     -- dump(bytes)  
  8.     local array = {}  
  9.     local len = #bytes  
  10.     for i = 1, len do  
  11.         -- table.insert(array, string.char(bytes[i]))  
  12.         array[i] = string.char(bytes[i]) -- 更快一些  
  13.     end  
  14.     -- dump(array)  
  15.     return table.concat(array)  
  16. end  
Protocol.strencode = function (str)
    --table.packAll(string.byte(str,1,#str))
    do return {string.byte(str,1,#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中的方法就可以解决中文乱码问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值