Skip to content

Commit 3476ee6

Browse files
committed
add 'rejoin' method. add 'immediate' as option on 'listen' method.
1 parent 0eb5000 commit 3476ee6

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed

colyseus/client.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function client:create_connection(path, options)
7171

7272
local params = { "colyseusid=" .. storage.get_item("colyseusid") }
7373
for k, v in pairs(options) do
74-
table.insert(params, k .. "=" .. v)
74+
table.insert(params, k .. "=" .. tostring(v))
7575
end
7676

7777
pprint(self.hostname .. path .. "?" .. table.concat(params, "&"))
@@ -99,12 +99,6 @@ function client:join(...)
9999
self.requestId = self.requestId + 1
100100
options.requestId = self.requestId;
101101

102-
-- get last session id for reconnection
103-
local reconnectionSessionId = storage.get_item("reconnection")
104-
if reconnectionSessionId ~= nil then
105-
options.sessionId = reconnectionSessionId
106-
end
107-
108102
local room = Room.create(roomName, options)
109103

110104
-- remove references on leaving
@@ -120,6 +114,17 @@ function client:join(...)
120114
return room
121115
end
122116

117+
function client:rejoin(roomName, sessionId)
118+
-- reopen client connection if it's closed
119+
if self.connection.state == "CLOSED" then
120+
self.connection:open()
121+
end
122+
123+
return self:join(roomName, {
124+
sessionId = sessionId
125+
})
126+
end
127+
123128
function client:on_batch_message(messages)
124129
for _, message in msgpack.unpacker(messages) do
125130
self:on_message(message)

colyseus/connection.lua

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,44 @@ end
1616

1717
function connection:init(endpoint)
1818
self._enqueuedCalls = {}
19+
self.endpoint = endpoint
1920

2021
self.is_html5 = sys.get_sys_info().system_name == "HTML5"
22+
self:open()
23+
end
24+
25+
function connection:loop(timeout)
26+
if self.ws then
27+
self.ws.step()
28+
socket.select(nil, nil, timeout or 0.001)
29+
end
30+
end
31+
32+
function connection:send(data)
33+
if self.ws and self.ws.state == "OPEN" then
34+
if self.is_html5 then
35+
-- binary frames are sent by default on HTML5
36+
self.ws:send(msgpack.pack(data))
37+
38+
else
39+
-- force binary frame on native platforms
40+
self.ws:send(msgpack.pack(data), 0x2)
41+
end
42+
43+
else
44+
-- WebSocket not connected.
45+
-- Enqueue data to be sent when readyState is OPEN
46+
table.insert(self._enqueuedCalls, { 'send', { data } })
47+
end
48+
end
49+
50+
function connection:open()
2151
self.ws = websocket_async()
2252

2353
self.ws:on_connected(function(ok, err)
2454
if err then
25-
print(err);
55+
self.state = self.ws.state
56+
2657
self:emit('error', err)
2758
self:close()
2859

@@ -42,35 +73,11 @@ function connection:init(endpoint)
4273
end)
4374

4475
self.ws:on_disconnected(function(e)
76+
self.state = self.ws.state
4577
self:emit("close", e)
4678
end)
4779

48-
self.ws:connect(endpoint)
49-
end
50-
51-
function connection:loop(timeout)
52-
if self.ws then
53-
self.ws.step()
54-
socket.select(nil, nil, timeout or 0.001)
55-
end
56-
end
57-
58-
function connection:send(data)
59-
if self.ws and self.ws.state == "OPEN" then
60-
if self.is_html5 then
61-
-- binary frames are sent by default on HTML5
62-
self.ws:send(msgpack.pack(data))
63-
64-
else
65-
-- force binary frame on native platforms
66-
self.ws:send(msgpack.pack(data), 0x2)
67-
end
68-
69-
else
70-
-- WebSocket not connected.
71-
-- Enqueue data to be sent when readyState is OPEN
72-
table.insert(self._enqueuedCalls, { 'send', { data } })
73-
end
80+
self.ws:connect(self.endpoint)
7481
end
7582

7683
function connection:close()

colyseus/room.lua

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function Room:init(name, options)
2727

2828
-- remove all listeners on leave
2929
self:on('leave', function()
30-
self:refresh_auto_reconnection()
3130
self:off()
3231
end)
3332
end
@@ -62,7 +61,6 @@ function Room:on_message (message)
6261

6362
if (code == protocol.JOIN_ROOM) then
6463
self.sessionId = message[2]
65-
self:refresh_auto_reconnection()
6664
self:emit("join")
6765

6866
elseif (code == protocol.JOIN_ERROR) then
@@ -87,10 +85,6 @@ function Room:on_message (message)
8785

8886
end
8987

90-
function Room:refresh_auto_reconnection()
91-
storage.set_item("reconnection", self.sessionId)
92-
end
93-
9488
function Room:setState (encodedState, remoteCurrentTime, remoteElapsedTime)
9589
local state = msgpack.unpack(encodedState)
9690

@@ -104,12 +98,12 @@ function Room:patch ( binaryPatch )
10498
-- apply patch
10599
self._previousState = fossil_delta.apply(self._previousState, binaryPatch)
106100

107-
local data = msgpack.unpack( utils.byte_array_to_string(self._previousState) )
101+
local new_state = msgpack.unpack( utils.byte_array_to_string(self._previousState) )
108102

109103
-- trigger state callbacks
110-
self:set( data )
104+
self:set( new_state )
111105

112-
self:emit("statechange", self.data)
106+
self:emit("statechange", self.state)
113107
end
114108

115109
function Room:leave()

colyseus/state_listener/state_container.lua

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ end
2525
StateContainer = {}
2626
local StateContainer_mt = { __index = StateContainer }
2727

28-
function StateContainer.new (data)
28+
function StateContainer.new (state)
2929
local instance = EventEmitter:new({
3030
defaultListener = nil,
3131
})
3232
setmetatable(instance, StateContainer_mt)
33-
instance:init(data)
33+
instance:init(state)
3434
return instance
3535
end
3636

37-
function StateContainer:init (data)
38-
self.data = data or {}
37+
function StateContainer:init (state)
38+
self.state = state or {}
3939

4040
self.matcherPlaceholders = {}
4141
self.matcherPlaceholders[":id"] = "^([%a%d-_]+)$"
@@ -47,18 +47,18 @@ function StateContainer:init (data)
4747
self:reset()
4848
end
4949

50-
function StateContainer:set (new_data)
51-
local patches = compare(self.data, new_data)
52-
self:check_patches(patches)
53-
self.data = new_data
50+
function StateContainer:set (new_state)
51+
local patches = compare(self.state, new_state)
52+
self:check_patches(patches, self._listeners, self.defaultListener)
53+
self.state = new_state
5454
return patches
5555
end
5656

5757
function StateContainer:register_placeholder (placeholder, matcher)
5858
self.matcherPlaceholders[placeholder] = matcher
5959
end
6060

61-
function StateContainer:listen (segments, callback)
61+
function StateContainer:listen (segments, callback, immediate)
6262
local rules
6363

6464
if type(segments) == "function" then
@@ -91,6 +91,10 @@ function StateContainer:listen (segments, callback)
9191
table.insert(self._listeners, listener)
9292
end
9393

94+
if immediate then
95+
self:check_patches(compare({}, self.state), { listener })
96+
end
97+
9498
return listener
9599
end
96100

@@ -106,16 +110,14 @@ function StateContainer:remove_all_listeners ()
106110
self:reset()
107111
end
108112

109-
function StateContainer:check_patches (patches)
110-
-- for (let i = patches.length - 1; i >= 0; i--) {
113+
function StateContainer:check_patches (patches, listeners, default_listener)
111114
for i = #patches, 1, -1 do
112115
local matched = false
113116

114-
-- for (let j = 0, len = this._listeners.length; j < len; j++) {
115117
local j = 1
116-
local total = #self._listeners
118+
local total = #listeners
117119
while j <= total do
118-
local listener = self._listeners[j]
120+
local listener = listeners[j]
119121
local path_variables = listener and self:get_path_variables(patches[i], listener)
120122

121123
if path_variables then
@@ -132,8 +134,8 @@ function StateContainer:check_patches (patches)
132134
end
133135

134136
-- check for fallback listener
135-
if (not matched and self.defaultListener) then
136-
self.defaultListener["callback"](patches[i])
137+
if (not matched and default_listener) then
138+
default_listener["callback"](patches[i])
137139
end
138140

139141
end

0 commit comments

Comments
 (0)