#Cocos2dx-Lua C++ socket 客户端
阻塞,每个SocketClient
直接创建一个线程阻塞调用recv
。
相关消息通知存入队列,由update
时发送到Lua
。
将 cpp 加入 lua_bindings 工程,使用lua_cocos2dx_socket_manual.hpp
的register_all_cocos2dx_socket_manual(L)
注册模块。
下面有一份简易的 Lua 代码:
function test:testSocket()
local client
client = sock.Cocos2dSocket:new(function(type, data, ext)
if type == sock.EventType.kState then
self:handleState(client, data, ext)
else
self:handleData(client, data)
end
end)
client:retain()
client:ConnectIP("127.0.0.1", 8080, sock.SOCK_STREAM)
end
function test:handleState(client, state, ext)
if state == sock.State.kConnected then
print("连接成功")
client:Send("hallo")
elseif state == sock.State.kClosed then
print("断开连接")
client:release()
end
end
function test:handleData(client, data)
print("received: " .. data)
end
Windows
平台还未调试