Skip to content

Commit

Permalink
external_api: add ability to send a text message through datachannels
Browse files Browse the repository at this point in the history
  • Loading branch information
horymury authored Mar 20, 2020
1 parent 14855f3 commit 4616065
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
18 changes: 17 additions & 1 deletion conference.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { openConnection } from './connection';

import { ENDPOINT_TEXT_MESSAGE_NAME } from './modules/API/constants';
import AuthHandler from './modules/UI/authentication/AuthHandler';
import Recorder from './modules/recorder/Recorder';

Expand Down Expand Up @@ -2041,7 +2042,22 @@ export default {

room.on(
JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
(...args) => APP.store.dispatch(endpointMessageReceived(...args)));
(...args) => {
APP.store.dispatch(endpointMessageReceived(...args));
if (args && args.length >= 2) {
const [ sender, eventData ] = args;

if (eventData.name === ENDPOINT_TEXT_MESSAGE_NAME) {
APP.API.notifyEndpointTextMessageReceived({
senderInfo: {
jid: sender._jid,
id: sender._id
},
eventData
});
}
}
});

room.on(
JitsiConferenceEvents.LOCK_STATE_CHANGED,
Expand Down
20 changes: 20 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ api.executeCommand('email', '[email protected]');
api.executeCommand('avatarUrl', 'https://avatars0.githubusercontent.com/u/3671647');
```

* **sendEndpointTextMessage** - Sends a text message to another participant through the datachannels.
```javascript
api.executeCommand('receiverParticipantId', 'text');
```

You can also execute multiple commands using the `executeCommands` method:
```javascript
api.executeCommands(commands);
Expand Down Expand Up @@ -323,6 +328,21 @@ changes. The listener will receive an object with the following structure:
}
```

* **endpointTextMessageReceived** - event notifications about a text message received through datachannels.
The listener will receive an object with the following structure:
```javascript
{
senderInfo: {
jid: string, // the jid of the sender
id: string // the participant id of the sender
},
eventData: {
name: string // the name of the datachannel event: `endpoint-text-message`
text: string // the received text from the sender
}
}
```

* **micError** - event notifications about Jitsi-Meet having failed to access the mic. The listener will receive an object with the following structure:
```javascript
{
Expand Down
27 changes: 26 additions & 1 deletion modules/API/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { invite } from '../../react/features/invite';
import { toggleTileView } from '../../react/features/video-layout';
import { getJitsiMeetTransport } from '../transport';

import { API_ID } from './constants';
import { API_ID, ENDPOINT_TEXT_MESSAGE_NAME } from './constants';
import {
processExternalDeviceRequest
} from '../../react/features/device-selection/functions';
Expand Down Expand Up @@ -155,6 +155,17 @@ function initCommands() {
'avatar-url': avatarUrl => {
sendAnalytics(createApiEvent('avatar.url.changed'));
APP.conference.changeLocalAvatarUrl(avatarUrl);
},
'send-endpoint-text-message': (to, text) => {
logger.debug('Send endpoint message command received');
try {
APP.conference.sendEndpointMessage(to, {
name: ENDPOINT_TEXT_MESSAGE_NAME,
text
});
} catch (err) {
logger.error('Failed sending endpoint text message', err);
}
}
};
transport.on('event', ({ data, name }) => {
Expand Down Expand Up @@ -437,6 +448,20 @@ class API {
});
}

/**
* Notify external application (if API is enabled) that user received
* a text message through datachannels.
*
* @param {Object} data - The event data.
* @returns {void}
*/
notifyEndpointTextMessageReceived(data: Object) {
this._sendEvent({
name: 'endpoint-text-message-received',
data
});
}

/**
* Notify external application (if API is enabled) that the device list has
* changed.
Expand Down
8 changes: 6 additions & 2 deletions modules/API/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ import parseURLParams from '../../react/features/base/config/parseURLParams';
/**
* JitsiMeetExternalAPI id - unique for a webpage.
*/
export const API_ID
= parseURLParams(window.location).jitsi_meet_external_api_id;
export const API_ID = parseURLParams(window.location).jitsi_meet_external_api_id;

/**
* The payload name for the datachannel/endpoint text message event
*/
export const ENDPOINT_TEXT_MESSAGE_NAME = 'endpoint-text-message';
2 changes: 2 additions & 0 deletions modules/API/external/external_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const commands = {
email: 'email',
hangup: 'video-hangup',
password: 'password',
sendEndpointTextMessage: 'send-endpoint-text-message',
sendTones: 'send-tones',
subject: 'subject',
submitFeedback: 'submit-feedback',
Expand All @@ -55,6 +56,7 @@ const events = {
'device-list-changed': 'deviceListChanged',
'display-name-change': 'displayNameChange',
'email-change': 'emailChange',
'endpoint-text-message-received': 'endpointTextMessageReceived',
'feedback-submitted': 'feedbackSubmitted',
'feedback-prompt-displayed': 'feedbackPromptDisplayed',
'filmstrip-display-changed': 'filmstripDisplayChanged',
Expand Down

0 comments on commit 4616065

Please sign in to comment.