@@ -3,32 +3,14 @@ local Room = require('colyseus.room')
33local protocol = require (' colyseus.protocol' )
44local EventEmitter = require (' colyseus.eventemitter' )
55local 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
268local client = { VERSION = " 0.8.0" }
279client .__index = client
2810
2911function 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
0 commit comments