MAF.Room
When you need to communicatie between different users of your app or between your app and a phone. The communication will take place through sockets.
new MAF.Room(id)
Parameters:
Name | Type | Description |
---|---|---|
id | String | Identifier to be used to construct the unique room hash. |
Members
connected :Boolean static
When your connected to the server it returns true
hash :String static
Unique hash that defines the room id.
joined :Boolean static
When your connected to the room it returns true
Methods
destroy(data)
Destroy the room. If your still connected it will disconnect.
Parameters:
Name Type Argument Description data Object optional Possibility to send some data to the room before disconnecting.
join(data)
Join the room
Parameters:
Name Type Argument Description data Object optional Data to be broadcasted to every connection to the room.
leave(data)
Leave the room.
Parameters:
Name Type Argument Description data Object optional Data to be broadcasted to every connection to the room.
send(data)
Send data to the room.
Parameters:
Name Type Description data Object Data to be broadcasted to every connection to the room.
Events
The events on this Instance are subcribed through the function.prototype.subscribeTo method.
var room = new MAF.Room('MyUniqueIdentifier'); (function (event) { console.log(event.type, event.payload); }).subscribeTo(room, ['onConnected', 'onDisconnected', 'onCreated', 'onDestroyed', 'onJoined', 'onHasLeft', 'onData', 'onError']); room.join();
onConnected
Fired when you connect to the room. The creator of the room wil not recieve this event.
onCreated
Fired only when the room is created.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash.
user String The user id that joined the room.
data Mixed Data send along when joining the room.
var room = new MAF.Room('MyUniqueIdentifier'); if (room.users.length > 0) room.join({msg: 'Client joined the room.'}); else room.join({msg: 'Room is created.'});
onData
Fired when someone sends some data to the room.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash.
user String The user id that joined the room.
data Mixed Data send by a user in the room.
onDestroyed
Fired when the room is destroyed.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash.
user String The user id that joined the room.
data Mixed Data that was send to the room when leaving the room.
onDisconnected
Fired when you disconnect from the room.
onError
Fired when there is a error.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash. Depending on the error this can be empty.
user String The user id that triggered this error, this can be empty depending on the error.
code Mixed The error code. Possible errors:
- 404 - Room doesn't exists.
- 405 - Not allowed to create Room.
- 429 - Too many connections.
onHasLeft
Fired when someone left the room.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash.
user String The user id that joined the room.
data Mixed Data that was send to the room when leaving the room.
Example
//Handling things when users leave. var room = new MAF.Room('MyUniqueIdentifier'); (function (event) { if (event.payload.user !== room.user) { console.log('A user has left the room:', event.payload.user, event.payload.data); } else { console.log('You have left the room.', event.payload.user, event.payload.data); } }).subscribeTo(room, ['onHasLeft']); //After being connected. room.leave('bye bye');
onJoined
Fired when someone has joined the room. This also includes the user that creates the room.
Parameters:
Name Type Description event Object The event object.
Properties
Name Type Description type String Event type.
payload Object Payload object containing event data.
Properties
Name Type Description hash String The room hash.
user String The user id that joined the room.
data Mixed Data send along when joining the room.
Example
//Handling things when users join. var room = new MAF.Room('MyUniqueIdentifier'); (function (event) { if (event.payload.user !== room.user) { console.log('A user has joined the room:', event.payload.user, event.payload.data); } else { console.log('You have joined the room.', event.payload.user, event.payload.data); } }).subscribeTo(room, ['onJoined']); room.join('Hello');