2020-10-20 19:29:41 +00:00
|
|
|
/**
|
|
|
|
* GsWebSocket to do Ghostream signalling
|
|
|
|
*/
|
|
|
|
export class GsWebSocket {
|
|
|
|
constructor() {
|
|
|
|
const protocol = (window.location.protocol === "https:") ? "wss://" : "ws://";
|
|
|
|
this.url = protocol + window.location.host + "/_ws/";
|
|
|
|
|
2020-10-22 16:21:42 +00:00
|
|
|
// Open WebSocket
|
2020-10-20 19:29:41 +00:00
|
|
|
this._open();
|
2020-10-22 16:21:42 +00:00
|
|
|
|
|
|
|
// Configure events
|
2020-10-20 19:45:26 +00:00
|
|
|
this.socket.addEventListener("open", () => {
|
2020-10-22 16:21:42 +00:00
|
|
|
console.log("[WebSocket] Connection established");
|
2020-10-20 19:29:41 +00:00
|
|
|
});
|
2020-10-20 19:45:26 +00:00
|
|
|
this.socket.addEventListener("close", () => {
|
2020-10-22 16:21:42 +00:00
|
|
|
console.log("[WebSocket] Connection closed, retrying connection in 1s...");
|
2020-10-21 20:38:36 +00:00
|
|
|
setTimeout(() => this._open(), 1000);
|
2020-10-20 19:29:41 +00:00
|
|
|
});
|
2020-10-20 19:45:26 +00:00
|
|
|
this.socket.addEventListener("error", () => {
|
2020-10-22 16:21:42 +00:00
|
|
|
console.log("[WebSocket] Connection errored, retrying connection in 1s...");
|
2020-10-21 20:38:36 +00:00
|
|
|
setTimeout(() => this._open(), 1000);
|
2020-10-20 19:29:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:21:42 +00:00
|
|
|
_open() {
|
|
|
|
console.log(`[WebSocket] Connecting to ${this.url}...`);
|
|
|
|
this.socket = new WebSocket(this.url);
|
|
|
|
}
|
|
|
|
|
2020-10-20 19:29:41 +00:00
|
|
|
/**
|
2020-10-22 16:21:42 +00:00
|
|
|
* Send local WebRTC session description to remote.
|
2020-10-21 20:10:39 +00:00
|
|
|
* @param {SessionDescription} localDescription WebRTC local SDP
|
|
|
|
* @param {string} stream Name of the stream
|
|
|
|
* @param {string} quality Requested quality
|
2020-10-20 19:29:41 +00:00
|
|
|
*/
|
2020-10-22 16:21:42 +00:00
|
|
|
sendLocalDescription(localDescription, stream, quality) {
|
2020-10-20 19:29:41 +00:00
|
|
|
if (this.socket.readyState !== 1) {
|
2020-10-22 16:21:42 +00:00
|
|
|
console.log("[WebSocket] Waiting for connection to send data...");
|
2020-10-23 06:11:55 +00:00
|
|
|
setTimeout(() => this.sendLocalDescription(localDescription, stream, quality), 100);
|
2020-10-20 19:29:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-22 16:41:14 +00:00
|
|
|
console.log(`[WebSocket] Sending WebRTC local session description for stream ${stream} quality ${quality}`);
|
2020-10-21 20:10:39 +00:00
|
|
|
this.socket.send(JSON.stringify({
|
|
|
|
"webRtcSdp": localDescription,
|
|
|
|
"stream": stream,
|
|
|
|
"quality": quality
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-22 16:21:42 +00:00
|
|
|
* Set callback function on new remote session description.
|
2020-10-21 20:10:39 +00:00
|
|
|
* @param {Function} callback Function called when data is received
|
|
|
|
*/
|
2020-10-22 16:21:42 +00:00
|
|
|
onRemoteDescription(callback) {
|
2020-10-20 19:29:41 +00:00
|
|
|
this.socket.addEventListener("message", (event) => {
|
2020-10-22 16:21:42 +00:00
|
|
|
console.log("[WebSocket] Received WebRTC remote session description");
|
2020-10-22 06:23:35 +00:00
|
|
|
const sdp = new RTCSessionDescription(JSON.parse(event.data));
|
|
|
|
callback(sdp);
|
2020-10-20 19:29:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|