Skip to content

Commit 66e8033

Browse files
committed
integrate new 'allowReconnection' feature from server. colyseus/colyseus#147
1 parent b985b7c commit 66e8033

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

colyseus/client.lua

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,14 @@ local Room = require('colyseus.room')
33
local protocol = require('colyseus.protocol')
44
local EventEmitter = require('colyseus.eventemitter')
55
local msgpack = require('colyseus.messagepack.MessagePack')
6-
7-
--
8-
-- Utility functions
9-
--
10-
local colyseus_id_file = sys.get_save_file("colyseus", "colyseusid")
11-
local function get_colyseus_id ()
12-
local data = sys.load(colyseus_id_file)
13-
-- if not next(my_table) then
14-
-- end
15-
return data[1] or ""
16-
end
17-
18-
local function set_colyseus_id(colyseus_id)
19-
local data = {}
20-
table.insert(data, colyseus_id)
21-
if not sys.save(colyseus_id_file, data) then
22-
print("colyseus.client: set_colyseus_id couldn't set colyseus_id locally.")
23-
end
24-
end
6+
local storage = require('colyseus.storage')
257

268
local client = { VERSION = "0.8.0" }
279
client.__index = client
2810

2911
function client.new (endpoint)
3012
local instance = EventEmitter:new({
31-
id = get_colyseus_id(),
13+
id = storage.get_item("colyseusid"),
3214
roomStates = {}, -- table
3315
rooms = {}, -- table
3416
connectingRooms = {}, -- table
@@ -51,7 +33,7 @@ function client:init(endpoint)
5133
self.connection = self:create_connection()
5234

5335
self.connection:on("open", function()
54-
if get_colyseus_id() ~= nil then
36+
if storage.get_item("colyseusid") ~= nil then
5537
self:emit("open")
5638
end
5739
end)
@@ -87,7 +69,7 @@ function client:create_connection(path, options)
8769
path = path or ""
8870
options = options or {}
8971

90-
local params = { "colyseusid=" .. get_colyseus_id() }
72+
local params = { "colyseusid=" .. storage.get_item("colyseusid") }
9173
for k, options in pairs(options) do
9274
table.insert(params, k .. "=" .. options[k])
9375
end
@@ -117,6 +99,12 @@ function client:join(...)
11799
self.requestId = self.requestId + 1
118100
options.requestId = self.requestId;
119101

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+
120108
local room = Room.create(roomName, options)
121109

122110
-- remove references on leaving
@@ -143,9 +131,9 @@ function client:on_message(message)
143131
local roomId = message[2]
144132

145133
if message[1] == protocol.USER_ID then
146-
set_colyseus_id(message[2])
147-
148134
self.id = message[2]
135+
storage.set_item("colyseusid", self.id)
136+
149137
self:emit('open')
150138

151139
elseif (message[1] == protocol.JOIN_ROOM) then

colyseus/room.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function Room:init(name, options)
2525
self.options = options or {}
2626

2727
-- remove all listeners on leave
28-
self:on('leave', self.off)
28+
self:on('leave', function()
29+
self:refresh_auto_reconnection()
30+
self:off()
31+
end)
2932
end
3033

3134
function Room:connect (connection)
@@ -58,6 +61,8 @@ function Room:on_message (message)
5861

5962
if (code == protocol.JOIN_ROOM) then
6063
self.sessionId = message[2]
64+
self.allow_reconnection = message[3]
65+
self:refresh_auto_reconnection()
6166
self:emit("join")
6267

6368
elseif (code == protocol.JOIN_ERROR) then
@@ -82,6 +87,12 @@ function Room:on_message (message)
8287

8388
end
8489

90+
function Room:refresh_auto_reconnection()
91+
if self.allow_reconnection then
92+
storage.set_item("reconnection", self.sessionId)
93+
end
94+
end
95+
8596
function Room:setState (encodedState, remoteCurrentTime, remoteElapsedTime)
8697
local state = msgpack.unpack(encodedState)
8798

colyseus/storage.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
local storage = {}
3+
local data = {}
4+
5+
local storage_file_path = sys.get_save_file("colyseus", "storage")
6+
7+
function storage.get_item (key)
8+
data = sys.load(storage_file_path)
9+
return data[key] or nil
10+
end
11+
12+
function storage.set_item (key, value)
13+
data[key] = value
14+
15+
if not sys.save(storage_file_path, data) then
16+
print("colyseus.client: storage.set_item couldn't set '" .. key .. "' locally.")
17+
end
18+
end
19+
20+
return storage

0 commit comments

Comments
 (0)