SocketClient.lua
3.57 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
cc.exports.SocketClient = class("SocketClient")
function SocketClient:getInstance()
local _gg = _G._gg
if _gg then
return _gg
end
_gg = SocketClient.new()
cc.exports._gg = _gg
setmetatable(_gg, self)
self.__index = self
return _gg
end
function SocketClient:ctor()
self.isFirstEnterGame_ = true
self.waittingCount_ = 0
self.handleList_ = {}
cmsg.on("gateway_msg.notify_exit_lobby_msg_res",handler(self,self.onLobbyExit));
EventListener.addEventListener(nil,EnumEvent.SOCKET_DISCONNECT,handler(self,self.onSocketDisConnect));
end
function SocketClient:onLobbyExit(params)
local function okFun()
cc.Director:getInstance():endToLua();
end
Alert.showOne(strings.msg_1001,okFun,okFun);
end
function SocketClient:onSocketDisConnect(params)
UserModel.isLogin = false;
if UserModel.online_reward_schedule_id then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(UserModel.online_reward_schedule_id);
UserModel.online_reward_schedule_id = nil;
end
if UserModel.update_time_schedule_id then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(UserModel.update_time_schedule_id);
UserModel.update_time_schedule_id = nil;
end
EventListener.dispatchEvent(EnumEvent.SOCKET_RECONNECT);
if not UserModel.isTick then
if socket_tcp.connected() then
socket_tcp.disconnect();
end
local function okFun()
DeviceUtil.restartApplication("",nil);
-- app:enterScene("LoadingScene",{nextSceneName="MainScene"});
end
Alert.showOne(strings.msg_1002,okFun,okFun);
end
end
function SocketClient:connect(host,port,connectSuccess,connectFaild)
self.onConnectSuccess = connectSuccess;
self.connectFaild = connectFaild;
-- addconnectaddr("10.100.6.189",26001);
addconnectaddr(host,port);
if self.handle_ then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.handle_)
self.handle_ = nil;
end
self.handle_ = cc.Director:getInstance():getScheduler():scheduleScriptFunc(handler(self, self.connectTimeout), TIMEOUT, false);
makeConnect(function()
if self.handle_ then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.handle_)
self.handle_ = nil;
end
print("SocketClient connect successful");
if self.onConnectSuccess then
self.onConnectSuccess();
self.onConnectSuccess = nil;
end
self.connectFaild = nil;
end,function ()
if self.handle_ then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.handle_)
self.handle_ = nil;
end
print("SocketClient connect faild");
if self.connectFaild then
self.connectFaild();
self.connectFaild = nil;
end
self.onConnectSuccess = nil;
end);
end
--连接超时
function SocketClient:connectTimeout(params)
if self.handle_ then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.handle_)
self.handle_ = nil;
end
if self.connectFaild then
self.connectFaild();
end
end
--[[向服务器发送消息
msg协议号对应的字符串 socket_msg.lua map key
data 发送的内容
]]--
function SocketClient:send(msg,data,timeOutFun)
cmsg.send(msg, data,timeOutFun);
if msg ~= "gateway_msg.sys_information_msg" then
logW(">>>>>>>>>>>>>>>\nMSGNAME:["..msg.."]\n DATA:"..json.encode(data));
end
end
return SocketClient