Skip to content

Commit b985b7c

Browse files
committed
rename DeltaContainer to StateContainer. fix socket timeout argument on connection loop.
1 parent fefdb57 commit b985b7c

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

colyseus/client.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ function client:init(endpoint)
5050

5151
self.connection = self:create_connection()
5252

53+
self.connection:on("open", function()
54+
if get_colyseus_id() ~= nil then
55+
self:emit("open")
56+
end
57+
end)
58+
5359
self.connection:on("message", function(message)
5460
self:on_batch_message(message)
5561
end)
@@ -90,11 +96,11 @@ function client:create_connection(path, options)
9096
return Connection.new(self.hostname .. path .. "?" .. table.concat(params, "&"))
9197
end
9298

93-
function client:loop()
94-
self.connection:loop()
99+
function client:loop(timeout)
100+
self.connection:loop(timeout)
95101

96102
for k, room in pairs(self.rooms) do
97-
room:loop()
103+
room:loop(timeout)
98104
end
99105
end
100106

@@ -111,7 +117,7 @@ function client:join(...)
111117
self.requestId = self.requestId + 1
112118
options.requestId = self.requestId;
113119

114-
local room = Room.create(roomName, options);
120+
local room = Room.create(roomName, options)
115121

116122
-- remove references on leaving
117123
room:on("leave", function()
@@ -133,7 +139,6 @@ function client:on_batch_message(messages)
133139
end
134140

135141
function client:on_message(message)
136-
137142
if type(message[1]) == "number" then
138143
local roomId = message[2]
139144

@@ -152,8 +157,8 @@ function client:on_message(message)
152157
return
153158
end
154159

155-
room.id = roomId;
156-
room:connect( self:create_connection(room.id, room.options) );
160+
room.id = roomId
161+
room:connect( self:create_connection(room.id, room.options) )
157162

158163
self.rooms[room.id] = room
159164
self.connectingRooms[ requestId ] = nil;

colyseus/connection.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local EventEmitter = require('colyseus.eventemitter')
33

44
local msgpack = require('colyseus.messagepack.MessagePack')
55
local websocket_async = require "websocket.client_async"
6-
-- local websocket_async = require "websocket.client_sync"
76

87
local connection = {}
98
connection.__index = connection
@@ -23,6 +22,7 @@ function connection:init(endpoint)
2322

2423
self.ws:on_connected(function(ok, err)
2524
if err then
25+
print(err);
2626
self:emit('error', err)
2727
self:close()
2828

@@ -42,18 +42,16 @@ function connection:init(endpoint)
4242
end)
4343

4444
self.ws:on_disconnected(function(e)
45-
print("DISCONNECTED!")
46-
pprint(e)
4745
self:emit("close", e)
4846
end)
4947

5048
self.ws:connect(endpoint)
5149
end
5250

53-
function connection:loop()
51+
function connection:loop(timeout)
5452
if self.ws then
5553
self.ws.step()
56-
socket.select(nil, nil, 0.5)
54+
socket.select(nil, nil, timeout or 0.001)
5755
end
5856
end
5957

colyseus/room.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ local msgpack = require('colyseus.messagepack.MessagePack')
22
local fossil_delta = require('colyseus.fossil_delta.fossil_delta')
33

44
local protocol = require('colyseus.protocol')
5-
local DeltaContainer = require('colyseus.delta_listener.delta_container')
5+
local StateContainer = require('colyseus.state_listener.state_container')
66

77
local utils = require('colyseus.utils')
88

99
Room = {}
1010
Room.__index = Room
1111

1212
function Room.create(name)
13-
local room = DeltaContainer.new()
13+
local room = StateContainer.new()
1414
setmetatable(room, Room)
1515
room:init(name)
1616
return room
1717
end
1818

19-
-- inherits from DeltaContainer
20-
setmetatable(Room, { __index = DeltaContainer })
19+
-- inherits from StateContainer
20+
setmetatable(Room, { __index = StateContainer })
2121

2222
function Room:init(name, options)
2323
self.id = nil
@@ -41,9 +41,9 @@ function Room:connect (connection)
4141
end)
4242
end
4343

44-
function Room:loop ()
44+
function Room:loop (timeout)
4545
if self.connection ~= nil then
46-
self.connection:loop()
46+
self.connection:loop(timeout)
4747
end
4848
end
4949

colyseus/delta_listener/compare.lua renamed to colyseus/state_listener/compare.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ local function generate(mirror, obj, patches, path)
3636
local changed = false
3737
local deleted = false
3838

39-
local t = #old_keys - 1
40-
while t >= 0 do
41-
local key = old_keys[t + 1]
39+
local t = #old_keys
40+
while t > 0 do
41+
local key = old_keys[t]
4242
local old_val = mirror[key]
4343

4444
if obj[key] ~= nil and not (obj[key] == nil and old_val ~= nil and not is_array(obj)) then
@@ -72,8 +72,8 @@ local function generate(mirror, obj, patches, path)
7272
return
7373
end
7474

75-
t = 1
76-
while t <= #new_keys do
75+
t = #new_keys
76+
while t > 0 do
7777
local key = new_keys[t]
7878

7979
if mirror[key] == nil and obj[key] ~= nil then
@@ -92,7 +92,7 @@ local function generate(mirror, obj, patches, path)
9292
})
9393
end
9494

95-
t = t + 1
95+
t = t - 1
9696
end
9797
end
9898

colyseus/delta_listener/delta_container.lua renamed to colyseus/state_listener/state_container.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local compare = require('colyseus.delta_listener.compare')
1+
local compare = require('colyseus.state_listener.compare')
22
local EventEmitter = require('colyseus.eventemitter')
33

44
local function split(str, delimiter)
@@ -22,19 +22,19 @@ local function map(array, func)
2222
return new_array
2323
end
2424

25-
DeltaContainer = {}
26-
local DeltaContainer_mt = { __index = DeltaContainer }
25+
StateContainer = {}
26+
local StateContainer_mt = { __index = StateContainer }
2727

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

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

4040
self.matcherPlaceholders = {}
@@ -47,18 +47,18 @@ function DeltaContainer:init (data)
4747
self:reset()
4848
end
4949

50-
function DeltaContainer:set (new_data)
50+
function StateContainer:set (new_data)
5151
local patches = compare(self.data, new_data)
5252
self:check_patches(patches)
5353
self.data = new_data
5454
return patches
5555
end
5656

57-
function DeltaContainer:register_placeholder (placeholder, matcher)
57+
function StateContainer:register_placeholder (placeholder, matcher)
5858
self.matcherPlaceholders[placeholder] = matcher
5959
end
6060

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

6464
if type(segments) == "function" then
@@ -94,19 +94,19 @@ function DeltaContainer:listen (segments, callback)
9494
return listener
9595
end
9696

97-
function DeltaContainer:remove_listener (listener)
97+
function StateContainer:remove_listener (listener)
9898
for k, l in ipairs(self._listeners) do
9999
if l == listener then
100100
table.remove(self._listeners, k)
101101
end
102102
end
103103
end
104104

105-
function DeltaContainer:remove_all_listeners ()
105+
function StateContainer:remove_all_listeners ()
106106
self:reset()
107107
end
108108

109-
function DeltaContainer:check_patches (patches)
109+
function StateContainer:check_patches (patches)
110110
-- for (let i = patches.length - 1; i >= 0; i--) {
111111
for i = #patches, 1, -1 do
112112
local matched = false
@@ -139,7 +139,7 @@ function DeltaContainer:check_patches (patches)
139139
end
140140
end
141141

142-
function DeltaContainer:get_path_variables (patch, listener)
142+
function StateContainer:get_path_variables (patch, listener)
143143
-- skip if rules count differ from patch
144144

145145
if #patch.path ~= #listener.rules then
@@ -167,8 +167,8 @@ function DeltaContainer:get_path_variables (patch, listener)
167167
return path
168168
end
169169

170-
function DeltaContainer:reset ()
170+
function StateContainer:reset ()
171171
self._listeners = {}
172172
end
173173

174-
return DeltaContainer
174+
return StateContainer

0 commit comments

Comments
 (0)