Constructor
new Room(socket, maxMembers, app, onRoomIsEmpty, onLeaderLeave)
Creates a new instance of Room.
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Founding socket. |
maxMembers |
number | Max number of members, that can be present in a room. |
app |
Object | Custom app, determines behaviour of a room. App has to implement onConnect, and recieveData methods, otherwise error is thrown. |
onRoomIsEmpty |
function | Callback function called, when a room gets empty. |
onLeaderLeave |
function | Callback function called, when leader leaves a room. |
Members
app
Properties:
| Name | Type | Description |
|---|---|---|
app |
Object | Custom app, determines behaviour of a room. App has to implement onConnect, and recieveData methods, otherwise error is thrown. |
id
Properties:
| Name | Type | Description |
|---|---|---|
id |
string | Unique identifier of room. |
maxMembers
Properties:
| Name | Type | Description |
|---|---|---|
maxMembers |
number | Maximum number of connected clients. |
members
Properties:
| Name | Type | Description |
|---|---|---|
members |
Array.<Object> | socket.io sockets array, constaining all connected clients. |
onLeaderLeave
Properties:
| Name | Type | Description |
|---|---|---|
onLeaderLeave |
function | Callback function called, when leader leaves a room. |
onRoomIsEmpty
Properties:
| Name | Type | Description |
|---|---|---|
onRoomIsEmpty |
function | Callback function called, when room gets empty. |
roomLeader
Properties:
| Name | Type | Description |
|---|---|---|
roomLeader |
Object | socket.io socket with expanded permissions (kick, change settings etc.). |
Methods
broadcast(emit, data)
This function is assigned to app.broadcast to enable app to send data to all of it's members. -> this.app.broadcast = (data) => {room.broadcast("app_data", data)}
Parameters:
| Name | Type | Description |
|---|---|---|
emit |
string | Emit name. |
data |
* | Data sent. |
connect(socket) → {Status}
Try to connect socket to this room
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Connecting socket. |
Returns:
Status object. Data property contains room.id.
- Type
- Status
getRoomOptions() → {Object}
Returns all relevant propreties of a this room in an object.
Returns:
{id, roomLeader, members, maxMembers}
- Type
- Object
leave(socket) → {Status}
No implemented yet. Disconnect socket from this room. Removes socket from members array. Calls this.onRoomIsEmpty() if no members are left. Calls this.onLeaderLeave() when leader leaves.
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Socket, that should disconnect. |
Returns:
Status object with no data property.
- Type
- Status
onConnect(socket) → {void}
Gets called, when a socket connects succesfully. Calls app.onConnect(socket.id) with socket id as a parameter, letting app know that a new member has joined. Sets socket.data.id to room id , retaining this information for future emits.
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Connecting socket. |
Returns:
- Type
- void
onLeave(socket) → {void}
No implemented yet. Called when member leaves. Calls app.onLeave(socket.id) with socket id as a parameter, letting app know that a new member has left.
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Socket, that disconnected. |
Returns:
- Type
- void
recieveData(socket, data)
This function gets called when data for app arrive. Calls this.app.recieveData(senderId, data).
Parameters:
| Name | Type | Description |
|---|---|---|
socket |
Object | Socket, that sent data. |
data |
* | Arriving data. |