Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 3473561

Browse files
authored
Send system info and close message no more than once. (#316)
Sometimes system info and close message may be sent to remote side more than once. It caused PeerConnection to be closed if a close message is sent in the middle of a call.
1 parent d727af2 commit 3473561

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

src/sdk/p2p/p2pclient.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ const P2PClient = function(configuration, signalingChannel) {
9595
const data = JSON.parse(message);
9696
if (data.type === 'chat-closed') {
9797
if (channels.has(origin)) {
98-
getOrCreateChannel(origin).onMessage(data);
98+
getOrCreateChannel(origin, false).onMessage(data);
9999
channels.delete(origin);
100100
}
101101
return;
102102
}
103103
if (self.allowedRemoteIds.indexOf(origin) >= 0) {
104-
getOrCreateChannel(origin).onMessage(data);
104+
getOrCreateChannel(origin, false).onMessage(data);
105105
} else {
106106
sendSignalingMessage(origin, 'chat-closed',
107107
ErrorModule.errors.P2P_CLIENT_DENIED);
@@ -186,7 +186,7 @@ const P2PClient = function(configuration, signalingChannel) {
186186
return Promise.reject(new ErrorModule.P2PError(
187187
ErrorModule.errors.P2P_CLIENT_NOT_ALLOWED));
188188
}
189-
return Promise.resolve(getOrCreateChannel(remoteId).publish(stream));
189+
return Promise.resolve(getOrCreateChannel(remoteId, true).publish(stream));
190190
};
191191

192192
/**
@@ -208,7 +208,7 @@ const P2PClient = function(configuration, signalingChannel) {
208208
return Promise.reject(new ErrorModule.P2PError(
209209
ErrorModule.errors.P2P_CLIENT_NOT_ALLOWED));
210210
}
211-
return Promise.resolve(getOrCreateChannel(remoteId).send(message));
211+
return Promise.resolve(getOrCreateChannel(remoteId, true).send(message));
212212
};
213213

214214
/**
@@ -263,13 +263,13 @@ const P2PClient = function(configuration, signalingChannel) {
263263
});
264264
};
265265

266-
const getOrCreateChannel = function(remoteId) {
266+
const getOrCreateChannel = function(remoteId, isInitializer) {
267267
if (!channels.has(remoteId)) {
268268
// Construct an signaling sender/receiver for P2PPeerConnection.
269269
const signalingForChannel = Object.create(EventDispatcher);
270270
signalingForChannel.sendSignalingMessage = sendSignalingMessage;
271271
const pcc = new P2PPeerConnectionChannel(config, myId, remoteId,
272-
signalingForChannel);
272+
signalingForChannel, isInitializer);
273273
pcc.addEventListener('streamadded', (streamEvent)=>{
274274
self.dispatchEvent(streamEvent);
275275
});

src/sdk/p2p/peerconnection-channel.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const sysInfo = Utils.sysInfo();
6060
class P2PPeerConnectionChannel extends EventDispatcher {
6161
// |signaling| is an object has a method |sendSignalingMessage|.
6262
/* eslint-disable-next-line require-jsdoc */
63-
constructor(config, localId, remoteId, signaling) {
63+
constructor(config, localId, remoteId, signaling, isInitializer) {
6464
super();
6565
this._config = config;
6666
this._localId = localId;
@@ -93,6 +93,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
9393
this._infoSent = false;
9494
this._disposed = false;
9595
this._createPeerConnection();
96+
if (isInitializer) {
97+
this._sendSignalingMessage(SignalingType.CLOSED);
98+
}
99+
this._sendSignalingMessage(SignalingType.UA, sysInfo);
96100
}
97101

98102
/**
@@ -114,9 +118,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
114118
ErrorModule.errors.P2P_CLIENT_INVALID_STATE,
115119
'All tracks are ended.'));
116120
}
117-
return Promise.all([this._sendClosedMsgIfNecessary(),
118-
this._sendSysInfoIfNecessary(),
119-
this._sendStreamInfo(stream)]).then(() => {
121+
return this._sendStreamInfo(stream).then(() => {
120122
return new Promise((resolve, reject) => {
121123
// Replace |addStream| with PeerConnection.addTrack when all browsers are ready.
122124
for (const track of stream.mediaStream.getTracks()) {
@@ -159,14 +161,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
159161
this._createDataChannel(DataChannelLabel.MESSAGE);
160162
}
161163

162-
this._sendClosedMsgIfNecessary().catch((err) => {
163-
Logger.debug('Failed to send closed message.' + err.message);
164-
});
165-
166-
this._sendSysInfoIfNecessary().catch((err) => {
167-
Logger.debug('Failed to send sysInfo.' + err.message);
168-
});
169-
170164
const dc = this._dataChannels.get(DataChannelLabel.MESSAGE);
171165
if (dc.readyState === 'open') {
172166
this._dataChannels.get(DataChannelLabel.MESSAGE).send(
@@ -242,7 +236,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
242236
switch (message.type) {
243237
case SignalingType.UA:
244238
this._handleRemoteCapability(message.data);
245-
this._sendSysInfoIfNecessary();
246239
break;
247240
case SignalingType.TRACK_SOURCES:
248241
this._trackSourcesHandler(message.data);
@@ -803,23 +796,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
803796
]);
804797
}
805798

806-
807-
_sendSysInfoIfNecessary() {
808-
if (this._infoSent) {
809-
return Promise.resolve();
810-
}
811-
this._infoSent = true;
812-
return this._sendSignalingMessage(SignalingType.UA, sysInfo);
813-
}
814-
815-
_sendClosedMsgIfNecessary() {
816-
if (this._pc.remoteDescription === null ||
817-
this._pc.remoteDescription.sdp === '') {
818-
return this._sendSignalingMessage(SignalingType.CLOSED);
819-
}
820-
return Promise.resolve();
821-
}
822-
823799
_handleRemoteCapability(ua) {
824800
if (ua.sdk && ua.sdk && ua.sdk.type === 'JavaScript' && ua.runtime &&
825801
ua.runtime.name === 'Firefox') {

0 commit comments

Comments
 (0)