Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout as option to dataconnection #798

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ declare namespace Peer {
metadata?: any;
serialization?: string;
reliable?: boolean;
heartbeatInterval?: number;
}

interface CallOption {
Expand Down
46 changes: 45 additions & 1 deletion lib/dataconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ConnectionType,
ConnectionEventType,
SerializationType,
ServerMessageType
ServerMessageType,
SocketSpecialMessagePrefix
} from "./enums";
import { Peer } from "./peer";
import { BaseConnection } from "./baseconnection";
Expand All @@ -24,6 +25,7 @@ export class DataConnection extends BaseConnection implements IDataConnection {
readonly label: string;
readonly serialization: SerializationType;
readonly reliable: boolean;
readonly heartbeatInterval: number;
stringify: (data: any) => string = JSON.stringify;
parse: (data: string) => any = JSON.parse;

Expand All @@ -44,6 +46,8 @@ export class DataConnection extends BaseConnection implements IDataConnection {

private _dc: RTCDataChannel;
private _encodingQueue = new EncodingQueue();
private _heartbeatSendTimer?: number;
private _heartbeatReceiveTimer?: number;

get dataChannel(): RTCDataChannel {
return this._dc;
Expand All @@ -60,6 +64,7 @@ export class DataConnection extends BaseConnection implements IDataConnection {
this.label = this.options.label || this.connectionId;
this.serialization = this.options.serialization || SerializationType.Binary;
this.reliable = !!this.options.reliable;
this.heartbeatInterval = this.options.heartbeatInterval || 0;

this._encodingQueue.on('done', (ab: ArrayBuffer) => {
this._bufferedSend(ab);
Expand Down Expand Up @@ -94,6 +99,9 @@ export class DataConnection extends BaseConnection implements IDataConnection {
logger.log(`DC#${this.connectionId} dc connection success`);
this._open = true;
this.emit(ConnectionEventType.Open);
if (this.heartbeatInterval > 0) {
this._enabledHeartbeat()
}
};

this.dataChannel.onmessage = (e) => {
Expand Down Expand Up @@ -135,6 +143,11 @@ export class DataConnection extends BaseConnection implements IDataConnection {
deserializedData = this.parse(data as string);
}

if (this.heartbeatInterval > 0 && deserializedData === `${SocketSpecialMessagePrefix.Heartbeat}${this.connectionId}`) {
this._restartHeartbeatReceiveTimeout()
return;
}

// Check if we've chunked--if so, piece things back together.
// We're guaranteed that this isn't 0.
if (deserializedData.__peerData) {
Expand Down Expand Up @@ -201,6 +214,8 @@ export class DataConnection extends BaseConnection implements IDataConnection {
this._encodingQueue = null;
}

this._disableHeartbeat();

if (!this.open) {
return;
}
Expand Down Expand Up @@ -312,6 +327,35 @@ export class DataConnection extends BaseConnection implements IDataConnection {
}
}

private _enabledHeartbeat() {
logger.log(`DC#${this.connectionId} Heartbeat Timer enabled`);
this._heartbeatSendTimer = window.setInterval(() => {
this.send(`${SocketSpecialMessagePrefix.Heartbeat}${this.connectionId}`)
}, this.heartbeatInterval)

this._restartHeartbeatReceiveTimeout();
}

private _restartHeartbeatReceiveTimeout() {
if (this._heartbeatReceiveTimer != null) {
window.clearTimeout(this._heartbeatReceiveTimer);
}
this._heartbeatReceiveTimer = window.setTimeout(() => {
logger.log(`DC#${this.connectionId} Disconnected due to heartbeat timeout`);
this.close();
}, this.heartbeatInterval * 2)
}

private _disableHeartbeat() {
if (this._heartbeatReceiveTimer != null) {
window.clearTimeout(this._heartbeatReceiveTimer);
}

if (this._heartbeatSendTimer != null) {
window.clearTimeout(this._heartbeatSendTimer);
}
}

handleMessage(message: ServerMessage): void {
const payload = message.payload;

Expand Down
4 changes: 4 additions & 0 deletions lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ export enum ServerMessageType {
Leave = "LEAVE", // Another peer has closed its connection to this peer.
Expire = "EXPIRE" // The offer sent to a peer has expired without response.

}

export enum SocketSpecialMessagePrefix {
Heartbeat = "HEARTBEAT_",
}
3 changes: 2 additions & 1 deletion lib/negotiator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ export class Negotiator {
...payload,
label: dataConnection.label,
reliable: dataConnection.reliable,
serialization: dataConnection.serialization
serialization: dataConnection.serialization,
heartbeatInterval: dataConnection.heartbeatInterval,
};
}

Expand Down
3 changes: 2 additions & 1 deletion lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ export class Peer extends EventEmitter {
metadata: payload.metadata,
label: payload.label,
serialization: payload.serialization,
reliable: payload.reliable
reliable: payload.reliable,
heartbeatInterval: payload.heartbeatInterval,
});
this._addConnection(peerId, connection);
this.emit(PeerEventType.Connection, connection);
Expand Down