1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2025-06-30 05:31:07 +02:00

Better javascript messages

This commit is contained in:
Alexandre Iooss
2020-10-22 18:21:42 +02:00
parent 24478bdc7a
commit 4cbb1d8192
3 changed files with 47 additions and 48 deletions

View File

@ -5,44 +5,42 @@ export class GsWebSocket {
constructor() {
const protocol = (window.location.protocol === "https:") ? "wss://" : "ws://";
this.url = protocol + window.location.host + "/_ws/";
// Open WebSocket
this._open();
// Configure events
this.socket.addEventListener("open", () => {
console.log("[WebSocket] Connection established");
});
this.socket.addEventListener("close", () => {
console.log("[WebSocket] Connection closed, retrying connection in 1s...");
setTimeout(() => this._open(), 1000);
});
this.socket.addEventListener("error", () => {
console.log("[WebSocket] Connection errored, retrying connection in 1s...");
setTimeout(() => this._open(), 1000);
});
}
_open() {
console.log(`[WebSocket] Connecting to ${this.url}...`);
this.socket = new WebSocket(this.url);
}
/**
* Open websocket.
* @param {Function} openCallback Function called when connection is established.
* @param {Function} closeCallback Function called when connection is lost.
*/
open() {
this._open();
this.socket.addEventListener("open", () => {
console.log("WebSocket opened");
});
this.socket.addEventListener("close", () => {
console.log("WebSocket closed, retrying connection in 1s...");
setTimeout(() => this._open(), 1000);
});
this.socket.addEventListener("error", () => {
console.log("WebSocket errored, retrying connection in 1s...");
setTimeout(() => this._open(), 1000);
});
}
/**
* Exchange WebRTC session description with server.
* Send local WebRTC session description to remote.
* @param {SessionDescription} localDescription WebRTC local SDP
* @param {string} stream Name of the stream
* @param {string} quality Requested quality
*/
sendDescription(localDescription, stream, quality) {
sendLocalDescription(localDescription, stream, quality) {
if (this.socket.readyState !== 1) {
console.log("Waiting for WebSocket to send data...");
console.log("[WebSocket] Waiting for connection to send data...");
setTimeout(() => this.sendDescription(localDescription, stream, quality), 100);
return;
}
console.log("[WebSocket] Sending WebRTC local session description");
this.socket.send(JSON.stringify({
"webRtcSdp": localDescription,
"stream": stream,
@ -51,12 +49,12 @@ export class GsWebSocket {
}
/**
* Set callback function on new session description.
* Set callback function on new remote session description.
* @param {Function} callback Function called when data is received
*/
onDescription(callback) {
onRemoteDescription(callback) {
this.socket.addEventListener("message", (event) => {
console.log("Message from server ", event.data);
console.log("[WebSocket] Received WebRTC remote session description");
const sdp = new RTCSessionDescription(JSON.parse(event.data));
callback(sdp);
});