Socket
helloz 4/3/2024 Unity3d
functions
functions | parameter | describe |
---|---|---|
LuaHandler.Send(table,luafunction) | luatable,callback | table 必须有一个cmd字段: 如果这个cmd是双向的,则会触发callback,返回参数为 json 字符串 |
LuaHandler.Send(table) | luatable | table 必须有一个cmd字段:| |
Resident monitoring network communication function(常驻监听网络通信函数) | ||
LuaHandler.Regist(string,luafunction) | cmd,callback | 注册监听cmd,接收到消息后,触发回调,返回数据 json 字符串 |
LuaHandler.UnRegist(string,luafunction) | cmd,callback | 注销cmd 关联的方法 |
Network status
there may be network disconnection or connection status
Method 1:
Network status type is string
function Start()
print("Get the current network status:",LuaHandler.StatusStr);
luaEvent:AddEventListener("gms-woogi-event-network-connStatus",this,OnNetWork_v1);
end
function OnNetWork_v1(info)
if info:CheckData() then
--type => string
local status = info.Data;
--Disconnected,Connected
print("network","status",status)
if status == "Disconnected" then
print("+++++++++++++++++++++++","Disconnected")
elseif status == "Connected" then
print("+++++++++++++++++++++++","Connected")
end
end
end
function OnDestroy()
luaEvent:RemoveAllEventListener(this);
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Method 2:
The network status type is an enumeration
CS.SocketStatus.Disconnected
CS.SocketStatus.Connected
1
2
2
function Start()
print("Get the current network status:",LuaHandler.Status);
luaEvent:AddEventListener("gms-woogi-event-network-status",this,OnNetWork_v2);
end
function OnNetWork_v2(info)
if info:CheckData() then
local status = info.Data
--CS.SocketStatus.Disconnected,CS.SocketStatus.Connected
print("network_v2","status",status)
if status == CS.SocketStatus.Disconnected then
print("------------------------","Disconnected")
elseif status == CS.SocketStatus.Connected then
print("------------------------","Connected")
end
end
end
function OnDestroy()
luaEvent:RemoveAllEventListener(this);
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
set worker
Custom worker, if not set, the system's default worker will be used
local tab = {}
tab["cmd"]= "test.cmd"
tab["w_a"]={1,2000} -- worker
LuaHandler.Send(tab)
--{"cmd":"test.cmd","w_a":[1,2000]}
1
2
3
4
5
2
3
4
5
example
function test()
Log("hello:test")
local dataTable={}
dataTable['cmd'] = 'test.hello'
dataTable['room'] = 50
LuaHandler.Send(dataTable,call)
end
--result: json string
function call(jsondata)
Log("hello:call==>"..jsondata)
local resultTable= jsonDecode(jsondata)
Log(resultTable['room'])
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
function Start()
LuaHandler.Regist('test.hello',onhello)
LuaHandler.Regist('test.bye-bye',onbyebye)
end
function Test()
local dataTable={}
dataTable['cmd'] = 'test.hello'
dataTable['userid'] = 100
LuaHandler.Send(dataTable)
end
function onhello(jsondta)
Log("hello:call==>"..jsondta)
local resultTable= jsonDecode(jsondta)
Log(resultTable['userid'])
end
function onbyebye(jsondta)
Log("byebye:call==>"..jsondta)
end
function OnDestory()
LuaHandler.UnRegist('test.hello',onhello)
LuaHandler.UnRegist('test.bye-bye',onbyebye)
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26