PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://gitlab.com/AmbassadorsLab/Timesync-Client
Markdown | 538 lines | 360 code | 178 blank | 0 comment | 0 complexity | 7853bc9e0017f5ce2489feea12a28e32 MD5 | raw file
  1. # Timesync-Client
  2. Javascript Client for the Ambassadors Timesync Server.
  3. - [TIMESYNC.Client](#client)
  4. * [Messages](#client.messages)
  5. * [Properties](#client.properties)
  6. * [Methods](#client.methods)
  7. * [Events](#client.events)
  8. - [TIMESYNC.Message](#message)
  9. * [Methods](#message.methods)
  10. - [TIMESYNC.util](#util)
  11. * [Methods](#util.methods)
  12. ## <a name="client"></a>TIMESYNC.Client ##
  13. The Timesync Client is used to establish a websocket connection to the Timesync Server, which in turn initiates a series of ping requests to establish the time offset between the server and the client, regardless of network delay or instability. Once the server has reached confidence it has established the offset, it will notify the client and pass it the offset.
  14. The Client and Server communicate with eachother by sending Messages over the websocket connection. In general, Messages contain a unique id, a timestamp that gets set when dispatched, a message type and message body. The type is used to trigger specific behaviour on either end and the body contains any type of JSON serializable content. The Client can either register message handlers to act on incoming messages, or attach a callback and scope to outgoing messages.
  15. Apart from establishing an accurate sync, the server exposes several other features. It incorporates 'rooms' which can be created or joined. Rooms can also store arbitrary properties. This is great when you want to save application state onto the room, which then can be accessed by other clients when they join the room. State changes are always broadcasted to all users in the same room.
  16. The Server will act on a specific set of Message types as described below, but will broadcast any 'unknown' messages to all users of the same room. This is great to instantly push application state from any user to all other users. Think of a play / pause event, or game start event etc.
  17. Currently the following Server communication is supported:
  18. ### <a name="client.messages"></a>Messages:
  19. - [clockOffset](#server.clockoffset) _(private)_
  20. - [echo](#server.echo)
  21. - [get_rooms](#server.get_rooms)
  22. - [get_room_properties](#server.get_room_properties)
  23. - [join_room](#server.join_room)
  24. - [leave_room](#server.leave_room)
  25. - [ping](#server.ping) _(private)_
  26. - [pong](#server.pong) _(private)_
  27. - [request_new_room](#server.request_new_room)
  28. - [room_properties_changed](#server.room_properties_changed) _(private)_
  29. - [set_room_properties](#server.set_room_properties)
  30. - [userCount](#server.set_room_properties) _(private)_
  31. ===
  32. <a name="server.clockoffset"></a>**clockOffset** ( ) : Integer _(private)_
  33. A Server initiated message that gets sent as soon as the sync offset has been determined.
  34. Body:
  35. - offset : Integer
  36. ===
  37. <a name="server.echo"></a>**echo** (...) : Anything JSON serializable
  38. The echo message is a simple debug message type intended for testing server / client response. The server will simply bounce the message back to the client.
  39. ===
  40. <a name="server.get_rooms"></a>**get_rooms** ( ) : Array
  41. Requests a list of all available rooms. Use a callback or register a 'get_rooms' message handler before dispatching the message.
  42. Body:
  43. - rooms : Array
  44. ===
  45. <a name="server.get_room_properties"></a>**get_room_properties** ( ) : Object
  46. Requests the current properties of the room. Use a callback or register a 'get_room_properties' message handler before dispatching the message.
  47. Body:
  48. - properties : Object
  49. ===
  50. <a name="server.join_room"></a>**join_room** (room) : Boolean
  51. Requests to join a specific room. If the room does not exist, it will be created. Use a callback or register a 'join_room' message handler before dispatching the message.
  52. Body:
  53. - success : Boolean
  54. ===
  55. <a name="server.leave_room"></a>**leave_room** ( ) : Boolean
  56. Requests to leave the current room. Use a callback or register a 'leave_room' message handler before dispatching the message.
  57. Body:
  58. - success : Boolean
  59. ===
  60. <a name="server.ping"></a>**ping** ( ) _(private)_
  61. Internal message type used for sync establishment, initiated by the Server.
  62. ===
  63. <a name="server.pong"></a>**pong** ( ) _(private)_
  64. Internal message type used for sync establishment, responded by the Client.
  65. ===
  66. <a name="server.request_new_room"></a>**request_new_room** ( )
  67. Request the server to create a new room and join it automatically. The room id will be generated by the server to ensure that it is unique. Use a callback or register a 'request_new_room' message handler before dispatching the message.
  68. Body:
  69. - room : room ID
  70. ===
  71. <a name="server.room_properties_changed"></a>**room_properties_changed** ( ) : Object _(private)_
  72. A Server initiated message to indicate that some or all room properties have changed.
  73. Body:
  74. - properties : Object
  75. ===
  76. <a name="server.set_room_properties"></a>**set_room_properties** (properties) : Boolean
  77. Request the server to store or update the given properties onto the room.
  78. ===
  79. <a name="server.userCount"></a>**userCount** ( ) : Integer _(private)_
  80. A Server initiated message to notify the current user count. This gets triggered everytime a user joins or leaves the room.
  81. Body:
  82. - count : Integer
  83. ===
  84. ### <a name="client.properties"></a>Properties:
  85. - debug : Boolean, enable debug messages.
  86. - server : String, the server address to connect to.
  87. - port : Integer, the server port to connect on.
  88. ===
  89. ### <a name="client.methods"></a>Methods:
  90. - [addListener](#client.addlistener)
  91. - [clock](#client.clock)
  92. - [connect](#client.connect)
  93. - [disconnect](#client.disconnect)
  94. - [fireEvent](#client.fireevent)
  95. - [getClockOffset](#client.getclockoffset)
  96. - [getConnected](#client.getconnected)
  97. - [getConnection](#client.getconnection)
  98. - [getId](#client.getid)
  99. - [getSyncProgress](#client.getsyncprogress)
  100. - [isSync](#client.issync)
  101. - [log](#client.log)
  102. - [newMsg](#client.newmsg)
  103. - [now](#client.now)
  104. - [registerHandler](#client.registerhandler)
  105. - [removeListener](#client.removelistener)
  106. - [send](#client.send)
  107. ===
  108. <a name="client.addlistener"></a>**addListener** (type, listener) : Client
  109. Adds an event listener to the client. For a list of events take a look at the [Events](#client.events) section.
  110. Parameters:
  111. - type : String
  112. - listener : Function
  113. Returns:
  114. - Client
  115. ===
  116. <a name="client.clock"></a>**clock** ( ) : Integer
  117. Returns the offset corrected current time. If the offset has not been established a warning will be printed in the logs.
  118. Returns:
  119. - Integer
  120. ===
  121. <a name="client.connect"></a>**connect** (server, port) : Websocket Connection
  122. Initiates a connection to the server. If no server and port parameters are provided it will grab them from the initial client config.
  123. Parameters:
  124. - server : String (optional)
  125. - port : Integer (optional)
  126. Returns:
  127. - Websocket Connection
  128. ===
  129. <a name="client.disconnect"></a>**disconnect** ( )
  130. Closes the server connection.
  131. ===
  132. <a name="client.fireevent"></a>**fireEvent** (event, details) : Client
  133. Method to fire an event. Any prior registered event listeners will be executed.
  134. Parameters:
  135. - event : String
  136. - details : Anything
  137. Returns:
  138. - Client
  139. ===
  140. <a name="client.getclockoffset"></a>**getClockOffset** ( ) : Integer
  141. Returns the time offset as estimated by the server
  142. Returns:
  143. - Integer
  144. ===
  145. <a name="client.getconnected"></a>**getConnected** ( ) : Boolean
  146. Method to return the connection state. Returns true if connected, false if not.
  147. Returns:
  148. - Boolean
  149. ===
  150. <a name="client.getconnection"></a>**getConnection** ( ) : Websocket Connection
  151. Method to return the current websocket connection.
  152. Returns:
  153. - Websocket Connection
  154. ===
  155. <a name="client.getid"></a>**getId** ( ) : UUID
  156. Method to return the ID of the Client instance.
  157. Returns:
  158. - String
  159. ===
  160. <a name="client.getsyncprogress"></a>**getSyncProgress** ( ) : Float
  161. Method to return the current time syncing progress. Note: alternatively one could use the 'syncprogress' event to receive regular updates on the sync progress.
  162. Returns:
  163. - Float
  164. ===
  165. <a name="client.issync"></a>**isSync** ( ) : Boolean
  166. Method to return the sync state. Returns true if a sync has been established, false if not.
  167. Returns:
  168. - Boolean
  169. ===
  170. <a name="client.log"></a>**log** (arguments)
  171. Convenience method to log console messages that will be honor the debug state as defined in the Client config.
  172. ===
  173. <a name="client.newmsg"></a>**newMsg** (type, body, callback, scope) : Message
  174. Convenience method to create a new message object and automatically bind it to the client. Optionally it allows for attaching a callback method and scope.
  175. ```
  176. tsClient = new TIMESYNC.Client();
  177. tsClient.connect("live.theambassadors.nl", 8080);
  178. tsClient.newMsg('join_room',
  179. {roomId: 'test'},
  180. function(result) {
  181. console.debug(result);
  182. },
  183. this
  184. ).send()
  185. ```
  186. Parameters:
  187. - type : String
  188. - body : Anything JSON serializable
  189. - callback : Function (optional)
  190. - scope : this reference (optional)
  191. Returns:
  192. - Message object
  193. ===
  194. <a name="client.now"></a>**now** ( ) : Integer
  195. Convenience method to get the current time in milliseconds without offset. Note: use clock() to get the current time with offset correction.
  196. Returns:
  197. - Integer
  198. ===
  199. <a name="client.registerhandler"></a>**registerHandler** (type, handler) : Client
  200. Method to register Message handlers. For a full list of supported Messages please see the [Messages](#client.messages) section.
  201. Parameters:
  202. - type : String
  203. - handler : Function
  204. Returns:
  205. - Client
  206. ===
  207. <a name="client.removelistener"></a>**removeListener** (type, listener)
  208. Removes a previously registere event listener. For a list of events take a look at the [Events](#client.events) section.
  209. Parameters:
  210. - type : String
  211. - listener : Function
  212. ===
  213. <a name="client.send"></a>**send** (msg)
  214. Method to send a Message to the server. The 'msg' can be supplied as an instantiated Message object, a config object or JSON formatted string.
  215. Parameters:
  216. - msg : Message / Object / JSON
  217. ===
  218. ### <a name="client.events"></a>Events:
  219. - [connected](#events.connected)
  220. - [connectionerror](#events.connectionerror)
  221. - [disconnected](#events.disconnected)
  222. - [syncestablished](#events.syncestablished)
  223. - [syncprogress](#events.syncprogress)
  224. - [usercount](#events.usercount)
  225. <a name="events.connected"></a>**connected** ( )
  226. Fires when the client has successfully initiated a server connection.
  227. ==
  228. <a name="events.connectionerror"></a>**connectionerror** (e)
  229. Fires when the client has successfully initiated a server connection.
  230. Parameters:
  231. - e : Error
  232. ===
  233. <a name="events.disconnected"></a>**disconnected** (e)
  234. Fires when the client has been disconnected from the server.
  235. ===
  236. <a name="events.syncestablished"></a>**syncestablished** (offset)
  237. Fires when the client and server have successfully estimated the time offset.
  238. Parameters:
  239. - offset : Integer
  240. ===
  241. <a name="events.syncprogress"></a>**syncprogress** (progress)
  242. Fires when the sync progress changes.
  243. Parameters:
  244. - progress : Float
  245. ===
  246. <a name="events.usercount"></a>**usercount** (count)
  247. Fires when the usercount changes. This happends when people join or leave the room.
  248. Parameters:
  249. - count : Integer
  250. ===
  251. ## <a name="message"></a>TIMESYNC.Message ##
  252. Message protocol inspired by 0MQ. Each Message instance contains a unique identifier and is structured with a head and body as follows:
  253. ```
  254. Message {
  255. id: uuid4 (String),
  256. head: {
  257. type: message type (String),
  258. ts: timestamp (Integer),
  259. clientId: uuid (String)
  260. },
  261. body: {
  262. ...
  263. }
  264. }
  265. ```
  266. ### <a name="message.methods"></a>Methods:
  267. - [bind](#message.bind)
  268. - [getBody](#message.getbody)
  269. - [getClient](#message.getclient)
  270. - [getTS](#message.getts)
  271. - [getType](#message.gettype)
  272. - [send](#message.send)
  273. - [setBody](#message.setbody)
  274. - [setTs](#message.setts)
  275. - [setType](#message.settype)
  276. - [validate](#message.validate)
  277. ===
  278. <a name="message.bind"></a>**bind** (client) : Message
  279. Method to bind the message to the client. This is needed so it can be sent to the server thru the client.
  280. Parameters:
  281. - client (Client)
  282. Returns:
  283. - Message
  284. ===
  285. <a name="message.getbody"></a>**getBody** ( ) : Anything JSON serializable
  286. Convenience method to get the body contents of a message object.
  287. ===
  288. <a name="message.getclient"></a>**getClient** ( ) : Client
  289. Method to get the client of this message, if it has been bound before.
  290. Returns:
  291. - Client
  292. ===
  293. <a name="message.getts"></a>**getTs** ( ) : Integer
  294. Convenience method to get the message timestamp.
  295. Returns:
  296. - Integer
  297. ===
  298. <a name="message.gettype"></a>**getType** ( ) : String
  299. Convenience method to get the type of a message object.
  300. Returns:
  301. - String
  302. ===
  303. <a name="message.send"></a>**send** ( ) : Message
  304. Method to send the message to the server. Note: the message needs to be bound to the client with the 'bind' method first.
  305. Returns:
  306. - Message
  307. ===
  308. <a name="message.setbody"></a>**setBody** (body) : Message
  309. Convenience method to directly set the body contents of a message object.
  310. Parameters:
  311. - body (Anything JSON serializable)
  312. Returns:
  313. - Message
  314. ===
  315. <a name="message.setts"></a>**setTs** (timestamp) : Message
  316. Convenience method to set the dispatch timestamp of a message. The timestamp lives in the message head. This rarely needs to bet set directly though, as it is set as soon as the message is sent to the server, which is what you normally would want.
  317. Parameters:
  318. - timestamp (Integer)
  319. Returns:
  320. - Message
  321. ===
  322. <a name="message.settype"></a>**setType** (type) : Message
  323. Convenience method to set the type of the message object.
  324. Parameters:
  325. - type (String)
  326. Returns:
  327. - Message
  328. ===
  329. <a name="message.validate"></a>**validate** ( ) : Message
  330. A validation method to make sure the message is poperly formatted. It will throw up errors when it encounters problems, but always returns the Message object so it can be used in concatenation.
  331. Returns:
  332. - Message
  333. ===
  334. ## <a name="util"></a>TIMESYNC.util ##
  335. General utility methods.
  336. ### <a name="util.methods"></a>Methods:
  337. - [capitaliseString](#util.capitalisestring)
  338. - [formatTime](#util.formattime)
  339. - [uuid4](#util.uuid4)
  340. ===
  341. <a name="util.capitalisestring"></a>**capitaliseString** (string) : String
  342. Method to promote the first character of a string to uppercase.
  343. Parameters:
  344. - string : String
  345. Returns:
  346. - String
  347. ===
  348. <a name="util.formattime"></a>**formatTime** (milliseconds) : String
  349. Formats an integer as milliseconds into a time string like: hours:minutes:seconds:milliseconds
  350. Parameters:
  351. - milliseconds : Integer
  352. Returns:
  353. - String
  354. ===
  355. <a name="util.uuid4"></a>**uuid4** ( ) : String
  356. Generates a Unique Universal Identifier.
  357. For example: ```91bb1fe1-e80e-452e-b506-2a83002caf78```
  358. Returns:
  359. - String
  360. ===