WebGroup
This class is an API starting point. It represents a peer to peer network, simply called a group. Each group member can send/receive broadcast as well as personal messages, invite other persons or bots (see Bot).
Example:
// Create a WebGroup with full mesh topology, autorejoin feature and
// specified Signaling and ICE servers for WebRTC.
const wg = new WebGroup({
signalingServer: 'wss://mysignaling.com',
rtcConfiguration: {
iceServers: [
{
urls: 'stun.l.google.com:19302'
},
{
urls: ['turn:myturn.com?transport=udp', 'turn:myturn?transport=tcp'],
username: 'user',
password: 'password'
}
]
}
})
wg.onMemberJoin = (id) => {
// YOUR CODE...
}
wg.onMemberLeave = (id) => {
// YOUR CODE...
}
wg.onMessage = (id, data) => {
// YOUR CODE...
}
wg.onStateChange = (state) => {
// YOUR CODE...
}
wg.onSignalingStateChange = (state) => {
// YOUR CODE...
}
Constructor Summary
Public Constructor | ||
public |
constructor(options: WebGroupOptions) |
Member Summary
Public Members | ||
public |
autoRejoin: boolean Enable/Desable the auto rejoin feature. |
|
public |
id: number The read-only WebGroup identifier. |
|
public |
key: string The read-only group session identifier. |
|
public |
members: number[] The read-only array of all members including yourself (i.e. |
|
public |
myId: number The read-only your unique member identifier in the group. |
|
public |
neighbors: number[] The read-only array of neighbor members (i.e. |
|
public |
onMemberJoin: function(id: number) This handler is called when a new member with |
|
public |
onMemberLeave: function(id: number) This handler is called when a member with |
|
public |
This handler is called when a message has been received from the group. |
|
public |
onMyId: function(myId: number) This handler is called when |
|
public |
onSignalingStateChange: function(state: SignalingState) This handler is called when the signaling state has changed. |
|
public |
onStateChange: function(state: WebGroupState) This handler is called when the group state has changed. |
|
public |
signalingServer: string The read-only signaling server URL. |
|
public |
The read-only state of the signaling server. |
|
public |
The read-only state of the WebGroup connection. |
|
public |
Method Summary
Public Methods | ||
public |
invite(url: string) Invite a bot server to join this group. |
|
public |
join(key: string) Join the group identified by a key provided by one of the group member. |
|
public |
leave() Leave the group which means close channels with all members and connection with the Signaling server. |
|
public |
Broadcast a message to the group. |
|
public |
Send a message to a particular group member. |
Public Constructors
public constructor(options: WebGroupOptions) source
Params:
Name | Type | Attribute | Description |
options | WebGroupOptions |
|
|
options.topology | Topology |
|
|
options.signalingServer | string |
|
|
options.rtcConfiguration | RTCConfiguration |
|
|
options.autoRejoin | boolean |
|
Test:
Public Members
public autoRejoin: boolean source
Enable/Desable the auto rejoin feature.
Test:
- 🙂 🤖 - 2 members: client invites bot invite autoRejoin should be disabled
- 🙂 🙂 - 2 clients join autoRejoin should be disabled
- 🙂 🙂 - 2 clients leave autoRejoin should be disabled
- 🤖 - 1 bot leave autoRejoin should be disabled
- 🙂 - 1 client join autoRejoin should be disabled
- 🙂 - 1 client leave autoRejoin should be disabled
- 🤖 - 1 bot join autoRejoin should be disabled
- 🤖 🙂 - 2 members: bot first, then client join autoRejoin should be disabled
public id: number source
The read-only WebGroup identifier. The same value for all members.
Test:
- 🙂 🤖 - 2 members: client invites bot invite WebGroup id should not change, should be the same and not 0
- 🙂 🙂 - 2 clients join WebGroup id should not change, should be the same and not 0
- 🤖 - 1 bot join WebGroup id should not be 0
- 🙂 🙂 - 2 clients leave WebGroup id of the first client should NOT be 0 and of second should be 0
- 🤖 - 1 bot leave WebGroup id should be 0
- 🙂 - 1 client join WebGroup id should not be 0
- 🙂 - 1 client leave WebGroup id should be 0
- 🤖 🙂 - 2 members: bot first, then client join WebGroup id should not change, should be the same and not 0
public key: string source
The read-only group session identifier. Equals to an empty string before calling WebGroup#join. Different to WebGroup#id. This key is known and used by Signaling server in order to join new members, on the other hand Signaling does not know WebGroup#id.
Test:
- 🙂 🤖 - 2 members: client invites bot invite key should not change, should be the same and not empty
- 🙂 🙂 - 2 clients join key should not change, should be the same and not empty
- 🤖 - 1 bot join key should be the one provided to the join method
- 🙂 🙂 - 2 clients leave key of the first client should NOT be empty and of second should be empty
- 🤖 - 1 bot leave key should be empty
- 🙂 - 1 client join key should not be empty
- 🙂 - 1 client leave key should be empty
- 🤖 🙂 - 2 members: bot first, then client join key should not change, should be the same and not empty
public members: number[] source
The read-only array of all members including yourself (i.e. WebGroup#myId)
Test:
- 🙂 🤖 - 2 members: client invites bot invite should have 2 members
- 🤖 - 1 bot join should have only me as a member
- 🙂 🙂 - 2 clients join should have 2 members
- 🙂 🙂 - 2 clients leave first client should have only him as a member and second client should have no members
- 🤖 - 1 bot leave should have no members
- 🙂 - 1 client join should have only me as a member
- 🙂 - 1 client leave should have no members
- 🤖 🙂 - 2 members: bot first, then client join should have 2 members
public myId: number source
The read-only your unique member identifier in the group.
Test:
- 🙂 🤖 - 2 members: client invites bot invite client's id should not change and bot's id should not be 0
- 🤖 - 1 bot join my id should not be 0
- 🙂 🙂 - 2 clients join first client's id should not change and second client's id should not be 0
- 🙂 🙂 - 2 clients leave the id of the first client should NOT be 0 and of second should be 0
- 🤖 - 1 bot leave my id should be 0
- 🙂 - 1 client join my id should not be 0
- 🙂 - 1 client leave my id should be 0
- 🤖 🙂 - 2 members: bot first, then client join bot's id should not change and the client's id should not be 0
public neighbors: number[] source
The read-only array of neighbor members (i.e. members who are directly connected to you). Hence they are included in the WebGroup#members by definition.
public onMemberJoin: function(id: number) source
This handler is called when a new member with id
as identifier has joined the group.
Test:
- 🙂 🙂 - 2 clients join should be notified about new member
- 🙂 🙂 🙂 - 3 clients join should be notified about new member
- 🤖 - 1 bot leave should NOT be notified about new member
- 🤖 - 1 bot join should NOT be notified about new member
- 🙂 - 1 client join should NOT be notified about new member
- 🙂 - 1 client leave should NOT be notified about new member
- 🤖 🙂 - 2 members: bot first, then client join should be notified about new member
- 🙂 🤖 - 2 members: client invites bot invite should be notified about new member
public onMemberLeave: function(id: number) source
This handler is called when a member with id
as identifier hes left the group.
Test:
- 🙂 🤖 - 2 members: client invites bot invite should NOT be notified about left member
- 🙂 🙂 - 2 clients join should NOT be notified about left member
- 🙂 🙂 - 2 clients leave should be notified about left member
- 🙂 🙂 - 2 clients leave should NOT be notified about joined member
- 🤖 - 1 bot leave should NOT be notified about left member
- 🙂 - 1 client join should NOT be notified about left member
- 🤖 - 1 bot join should NOT be notified about left member
- 🙂 - 1 client leave should NOT be notified about left member
- 🤖 🙂 - 2 members: bot first, then client join should NOT be notified about left member
public onMessage: function(id: number, data: DataType) source
This handler is called when a message has been received from the group.
id
is an identifier of the member who sent this message.
Test:
- 🙂 🤖 - 2 members: client invites bot invite should NOT receive any message
- 🙂 🙂 - 2 clients join should NOT receive any message
- 🙂 🙂 - 2 clients leave should NOT receive any message
- 🤖 - 1 bot leave should NOT receive any message
- 🙂 - 1 client join should NOT receive any message
- 🙂 - 1 client leave should NOT receive any message
- 🤖 - 1 bot join should NOT receive any message
- 🤖 🙂 - 2 members: bot first, then client join should NOT receive any message
public onMyId: function(myId: number) source
This handler is called when myId
is set.
Test:
- 🙂 🤖 - 2 members: client invites bot invite should be called
- 🙂 🙂 - 2 clients join should be called
- 🤖 - 1 bot leave should NOT be called
- 🙂 - 1 client join should be called
- 🙂 - 1 client leave should NOT be called
- 🤖 - 1 bot join should be called
- 🤖 🙂 - 2 members: bot first, then client join should be called
public onSignalingStateChange: function(state: SignalingState) source
This handler is called when the signaling state has changed.
Test:
- 🙂 🙂 - 2 clients join should change the Signaling state
- 🙂 🙂 - 2 clients leave should change the Signaling state
- 🙂 🙂 🙂 - 3 clients join should change the Signaling state
- 🤖 - 1 bot leave should change the Signaling state
- 🤖 - 1 bot join should change the Signaling state
- 🙂 - 1 client join should change the Signaling state
- 🙂 - 1 client leave should change the Signaling state
- 🤖 🙂 - 2 members: bot first, then client join should change the Signaling state
- 🙂 🤖 - 2 members: client invites bot invite should change the Signaling state
public onStateChange: function(state: WebGroupState) source
This handler is called when the group state has changed.
Test:
- 🙂 🙂 - 2 clients join should change the WebGroup state
- 🙂 🙂 - 2 clients leave should change the WebGroup state of the second client only
- 🙂 🙂 🙂 - 3 clients join should change the WebGroup state
- 🤖 - 1 bot leave should change the WebGroup state
- 🤖 - 1 bot join should change the WebGroup state
- 🙂 - 1 client join should change the WebGroup state
- 🙂 - 1 client leave should change the WebGroup state
- 🤖 🙂 - 2 members: bot first, then client join should change the WebGroup state
- 🙂 🤖 - 2 members: client invites bot invite should change the WebGroup state of the bot only
public signalingServer: string source
The read-only signaling server URL.
Test:
- 🙂 🤖 - 2 members: client invites bot invite Signaling server should not change
- 🙂 🙂 - 2 clients join Signaling server should not change
- 🙂 🙂 - 2 clients leave Signaling server should not change
- 🙂 - 1 client join Signaling server should not change
- 🙂 - 1 client leave Signaling server should not change
- 🤖 🙂 - 2 members: bot first, then client join Signaling server should not change
public signalingState: SignalingState source
The read-only state of the signaling server.
Test:
- 🙂 🙂 - 2 clients join Signaling state should be CHECKED
- 🙂 🙂 - 2 clients leave Signaling state of the first client should not be CLOSED and of the second should be CLOSED
- 🤖 - 1 bot join Signaling state should be CHECKED
- 🤖 - 1 bot leave Signaling state should be CLOSED
- 🙂 - 1 client join Signaling state should be CHECKED
- 🙂 - 1 client leave Signaling state should be CLOSED
- 🤖 🙂 - 2 members: bot first, then client join Signaling state should be CHECKED
- 🙂 🤖 - 2 members: client invites bot invite Signaling state should be CHECKED
public state: WebGroupState source
The read-only state of the WebGroup connection.
Test:
- 🙂 🙂 - 2 clients join WebGroup state should be JOINED
- 🙂 🙂 - 2 clients leave WebGroup state of the first client should be JOINED and of the second should be LEFT
- 🤖 - 1 bot join WebGroup state should be JOINED
- 🤖 - 1 bot leave WebGroup state should be LEFT
- 🙂 - 1 client join WebGroup state should be JOINED
- 🙂 - 1 client leave WebGroup state should be LEFT
- 🤖 🙂 - 2 members: bot first, then client join WebGroup state should be JOINED
- 🙂 🤖 - 2 members: client invites bot invite WebGroup state should be JOINED for both
public topology: Topology source
The read-only property which is an enum of type Topology indicating the topology used for this WebGroup instance.
Test:
- 🤖 - 1 bot join topology should not change
- 🙂 🤖 - 2 members: client invites bot invite topology should not change
- 🙂 🙂 - 2 clients join topology should not change
- 🙂 🙂 - 2 clients leave topology should not change
- 🤖 - 1 bot leave topology should not change
- 🙂 - 1 client join topology should not change
- 🙂 - 1 client leave topology should not change
- 🤖 🙂 - 2 members: bot first, then client join topology should not change
Public Methods
public invite(url: string) source
Invite a bot server to join this group.
Params:
Name | Type | Attribute | Description |
url | string | Bot server URL (See BotOptions) |
public join(key: string) source
Join the group identified by a key provided by one of the group member. If the current WebGroup#state value is not WebGroupState#LEFT or WebGroup#signalingState value is not SignalingState.CLOSED, then do nothing.
Params:
Name | Type | Attribute | Description |
key | string |
|
Will be generated if not provided |
Test:
- 🤖 - 1 bot join
- 🙂 🙂 - 2 clients join
- 🙂 🙂 🙂 - 3 clients join
- 🙂 - 1 client join
- 🤖 🙂 - 2 members: bot first, then client join
- 🙂 🙂 - 2 clients join should join with a specified key
- 🤖 🙂 🙂 - 3 members: 1 bot and 2 clients join
- 🙂 🤖 🙂 - 3 members: 1 client, 1 bot and 1 client join
- 🙂 - 1 client join should join with a specified key
public leave() source
Leave the group which means close channels with all members and connection with the Signaling server.
public send(data: DataType) source
Broadcast a message to the group.
Params:
Name | Type | Attribute | Description |
data | DataType |
Test:
- 🙂 🤖 - 2 members: client invites bot invite should send/receive broadcast String
- 🙂 🤖 - 2 members: client invites bot invite should send/receive broadcast ArrayBuffer
- 🙂 🙂 - 2 clients should send/receive broadcast String
- 🙂 🙂 - 2 clients should send/receive broadcast ArrayBuffer
- 🙂 🙂 🙂 - 3 clients should send/receive broadcast String
- 🙂 🙂 🙂 - 3 clients should send/receive broadcast ArrayBuffer
- 🤖 🙂 - 2 members: bot first, then client should send/receive broadcast String
- 🤖 🙂 - 2 members: bot first, then client should send/receive broadcast ArrayBuffer
public sendTo(id: number, data: DataType) source
Send a message to a particular group member.
Params:
Name | Type | Attribute | Description |
id | number | Member identifier |
|
data | DataType | Message |
Test:
- 🙂 🤖 - 2 members: client invites bot invite should send/receive broadcast message cutted in chunks (> 15kb)
- 🙂 🤖 - 2 members: client invites bot invite should send/receive private String
- 🙂 🤖 - 2 members: client invites bot invite should send/receive private ArrayBuffer
- 🙂 🙂 - 2 clients should send/receive broadcast message cutted in chunks (> 15kb)
- 🙂 🙂 - 2 clients should send/receive private String
- 🙂 🙂 - 2 clients should send/receive private ArrayBuffer
- 🙂 🙂 🙂 - 3 clients should send/receive broadcast message cutted in chunks (> 15kb)
- 🙂 🙂 🙂 - 3 clients should send/receive private String
- 🙂 🙂 🙂 - 3 clients should send/receive private ArrayBuffer
- 🤖 🙂 - 2 members: bot first, then client should send/receive broadcast message cutted in chunks (> 15kb)
- 🤖 🙂 - 2 members: bot first, then client should send/receive private String
- 🤖 🙂 - 2 members: bot first, then client should send/receive private ArrayBuffer