@@ -27,36 +27,6 @@ local NET={
27
27
onlineCount = " _" ,
28
28
}
29
29
30
- local mesType = {
31
- Connect = true ,
32
- Self = true ,
33
- Broadcast = true ,
34
- Private = true ,
35
- Server = true ,
36
- }
37
-
38
- -- Parse json message
39
- local function _parse (res )
40
- res = JSON .decode (res )
41
- if res then
42
- if mesType [res .type ]then
43
- return res
44
- else
45
- MES .new (' warn' ,(" [%s] %s" ):format (res .type or " ?" ,res .reason or " [NO Message]" ))
46
- end
47
- end
48
- end
49
-
50
- -- WS close message
51
- local function _closeMessage (message )
52
- local mes = JSON .decode (message )
53
- if mes then
54
- MES .new (' info' ,(" %s %s|%s" ):format (text .wsClose ,mes .type or " " ,mes .reason or " " ))
55
- else
56
- MES .new (' info' ,(" %s %s" ):format (text .wsClose ,message ))
57
- end
58
- end
59
-
60
30
61
31
62
32
---- ----------------------<NEW HTTP API>
@@ -69,7 +39,7 @@ local function getMsg(request,timeout)
69
39
if type (mes .body )== ' string' and # mes .body > 0 then
70
40
return JSON .decode (mes .body )
71
41
else
72
- MES .new (' info' ," Oops! Server is down " )
42
+ MES .new (' info' ,text . serverDown )
73
43
return
74
44
end
75
45
else
@@ -98,7 +68,7 @@ function NET.getCode(email)
98
68
MES .new (' error' ,res .message ,5 )
99
69
end
100
70
else
101
- MES .new (' warn' ," Request failed " ,5 )
71
+ MES .new (' warn' ,text . requestFailed ,5 )
102
72
end
103
73
104
74
WAIT .interrupt ()
@@ -138,7 +108,7 @@ function NET.codeLogin(code)
138
108
MES .new (' error' ,res .message ,5 )
139
109
end
140
110
else
141
- MES .new (' warn' ," Request failed " ,5 )
111
+ MES .new (' warn' ,text . requestFailed ,5 )
142
112
end
143
113
144
114
WAIT .interrupt ()
@@ -176,7 +146,7 @@ function NET.setPW(code,pw)
176
146
MES .new (' error' ,res .message ,5 )
177
147
end
178
148
else
179
- MES .new (' warn' ," Request failed " ,5 )
149
+ MES .new (' warn' ,text . requestFailed ,5 )
180
150
end
181
151
182
152
WAIT .interrupt ()
@@ -229,7 +199,6 @@ function NET.autoLogin()
229
199
USER .rToken = res .data .refreshToken
230
200
USER .aToken = res .data .accessToken
231
201
NET .connectWS ()
232
- MES .new (' info' ," Login successed" ,5 )
233
202
SCN .go (' net_menu' )
234
203
WAIT .interrupt ()
235
204
return
@@ -251,7 +220,6 @@ function NET.autoLogin()
251
220
},
252
221
},6.26 )
253
222
if res then
254
- print (TABLE .dump (res ))
255
223
if res .code == 200 then
256
224
USER .rToken = res .data .refreshToken
257
225
USER .aToken = res .data .accessToken
@@ -305,7 +273,7 @@ function NET.pwLogin(email,pw)
305
273
MES .new (' error' ,res .message ,5 )
306
274
end
307
275
else
308
- MES .new (' warn' ," Request failed " ,5 )
276
+ MES .new (' warn' ,text . requestFailed ,5 )
309
277
end
310
278
311
279
WAIT .interrupt ()
321
289
322
290
---- ----------------------<NEW WS API>
323
291
local function wsSend (act ,data )
292
+ -- print("SEND ACT: "..act)
324
293
WS .send (' game' ,JSON .encode {
325
294
action = act ,
326
295
data = data ,
454
423
-- WS
455
424
function NET .connectWS ()
456
425
if WS .status (' game' )== ' dead' then
457
- WS .connect (' game' ,' ' ,{
458
- [' x-access-token' ]= USER .aToken ,
459
- },6 )
426
+ WS .connect (' game' ,' ' ,{[' x-access-token' ]= USER .aToken },6 )
460
427
TASK .new (NET .updateWS )
461
428
end
462
429
end
@@ -471,13 +438,19 @@ function NET.updateWS()
471
438
if op == ' ping' then
472
439
elseif op == ' pong' then
473
440
elseif op == ' close' then
474
- _closeMessage (message )
441
+ local res = JSON .decode (message )
442
+ MES .new (' info' ,(" $1 $2" ):repD (text .wsClose ,res and res .message or message ))
443
+ if res and res .message then LOG (res .message ) end
444
+ TEST .yieldUntilNextScene ()
445
+ while SCN .stack [# SCN .stack - 1 ]~= ' main' do SCN .pop () end
446
+ SCN .back ()
475
447
return
476
448
else
477
- local res = _parse (message )
449
+ local res = JSON . decode (message )
478
450
if res then
479
- if res .type == ' Connect' then
480
- MES .new (' info' ,' Connected!' )
451
+ -- print(("RECV ACT: $1 ($2)"):repD(res.action,res.type))
452
+ if res .type == ' Failed' then
453
+ MES .new (' warn' ," Request failed: " .. (res .reason or " /" ))
481
454
elseif res .action == 1100 then -- TODO
482
455
elseif res .action == 1101 then -- TODO
483
456
elseif res .action == 1102 then -- TODO
0 commit comments