File tree Expand file tree Collapse file tree 5 files changed +68
-1
lines changed
Expand file tree Collapse file tree 5 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,25 @@ const sfu = new SfuClient();
147147 sfu.disconnect ();
148148 sfu.state === SFU_CLIENT_STATE.DISCONNECTED; // true
149149 ` ` `
150+ - broadcast ()
151+ ` ` ` js
152+ // in the sender' s client
153+ sfu.broadcast("hello");
154+ ```
155+ ```js
156+ // in the clients of other members of that channel
157+ sfu.addEventListener("update", ({ detail: { name, payload } }) => {
158+ switch (name) {
159+ case "broadcast":
160+ {
161+ const { senderId, message } = payload;
162+ console.log(`${senderId} says: "${message}"`); // 87 says "hello"
163+ }
164+ return;
165+ // ...
166+ }
167+ });
168+ ```
150169- updateUpload()
151170 ```js
152171 const audioStream = await window.navigator.mediaDevices.getUserMedia({
Original file line number Diff line number Diff line change @@ -154,6 +154,19 @@ export class SfuClient extends EventTarget {
154154 return this . _state ;
155155 }
156156
157+ /**
158+ * @param message any JSON serializable object
159+ */
160+ broadcast ( message ) {
161+ this . _bus . send (
162+ {
163+ name : CLIENT_MESSAGE . BROADCAST ,
164+ payload : message ,
165+ } ,
166+ { batch : true }
167+ ) ;
168+ }
169+
157170 /**
158171 * @param {string } url
159172 * @param {string } jsonWebToken
@@ -508,7 +521,7 @@ export class SfuClient extends EventTarget {
508521 /**
509522 * dispatches an event, intended for the client
510523 *
511- * @param { "disconnect" | "info_change" | "track" | "error" } name
524+ * @param { "disconnect" | "info_change" | "track" | "error" | "broadcast" } name
512525 * @param [payload]
513526 * @fires SfuClient#update
514527 */
@@ -557,6 +570,9 @@ export class SfuClient extends EventTarget {
557570 */
558571 async _handleMessage ( { name, payload } ) {
559572 switch ( name ) {
573+ case SERVER_MESSAGE . BROADCAST :
574+ this . _updateClient ( "broadcast" , payload ) ;
575+ break ;
560576 case SERVER_MESSAGE . SESSION_LEAVE :
561577 {
562578 const { sessionId } = payload ;
Original file line number Diff line number Diff line change @@ -507,6 +507,17 @@ export class Session extends EventEmitter {
507507 */
508508 async _handleMessage ( { name, payload } ) {
509509 switch ( name ) {
510+ case CLIENT_MESSAGE . BROADCAST :
511+ {
512+ this . _broadcast ( {
513+ name : SERVER_MESSAGE . BROADCAST ,
514+ payload : {
515+ senderId : this . id ,
516+ message : payload ,
517+ } ,
518+ } ) ;
519+ }
520+ break ;
510521 case CLIENT_MESSAGE . CONSUMPTION_CHANGE :
511522 {
512523 /** @type {{ sessionId: number, states: Object<boolean> } } */
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ export const SERVER_REQUEST = {
2222} ;
2323
2424export const SERVER_MESSAGE = {
25+ /** Signals that the server wants to send a message to all the other members of that channel */
26+ BROADCAST : "BROADCAST" ,
2527 /** Signals the clients that one of the session in their channel has left. */
2628 SESSION_LEAVE : "SESSION_LEAVE" ,
2729 /** Signals the clients that the info (talking, mute,...) of one of the session in their channel has changed. */
@@ -38,6 +40,8 @@ export const CLIENT_REQUEST = {
3840} ;
3941
4042export const CLIENT_MESSAGE = {
43+ /** Signals that the client wants to send a message to all the other members of that channel */
44+ BROADCAST : "BROADCAST" ,
4145 /** Signals that the client wants to change how it consumes a track (like pausing or ending the download) */
4246 CONSUMPTION_CHANGE : "CONSUMPTION_CHANGE" ,
4347 /** Signals that the info (talking, mute,...) of this client has changed. */
Original file line number Diff line number Diff line change @@ -263,4 +263,21 @@ describe("Full network", () => {
263263 const [ closeEvent ] = await closeProm ;
264264 expect ( closeEvent . code ) . toBe ( SESSION_CLOSE_CODE . P_TIMEOUT ) ;
265265 } ) ;
266+ test ( "A client can broadcast arbitrary messages to other clients on a channel that does not have webRTC" , async ( ) => {
267+ const channelUUID = await network . getChannelUUID ( false ) ;
268+ const user1 = await network . connect ( channelUUID , 1 ) ;
269+ const user2 = await network . connect ( channelUUID , 2 ) ;
270+ const sender = await network . connect ( channelUUID , 3 ) ;
271+ const message = "hello" ;
272+ sender . sfuClient . broadcast ( message ) ;
273+ const prom1 = once ( user1 . sfuClient , "update" ) ;
274+ const prom2 = once ( user2 . sfuClient , "update" ) ;
275+ const [ [ event1 ] , [ event2 ] ] = await Promise . all ( [ prom1 , prom2 ] ) ;
276+ expect ( event1 . detail . name ) . toEqual ( "broadcast" ) ;
277+ expect ( event2 . detail . name ) . toEqual ( "broadcast" ) ;
278+ expect ( event1 . detail . payload . senderId ) . toBe ( sender . session . id ) ;
279+ expect ( event2 . detail . payload . senderId ) . toBe ( sender . session . id ) ;
280+ expect ( event1 . detail . payload . message ) . toBe ( message ) ;
281+ expect ( event2 . detail . payload . message ) . toBe ( message ) ;
282+ } ) ;
266283} ) ;
You can’t perform that action at this time.
0 commit comments