mirror of
https://github.com/owncast/owncast.git
synced 2024-10-10 19:16:02 +00:00
Fix websocket not reconnecting. For #136
This commit is contained in:
parent
206d57e79e
commit
ce8fc019ec
@ -29,7 +29,13 @@ export default class Websocket {
|
|||||||
this.rawMessageListeners = [];
|
this.rawMessageListeners = [];
|
||||||
|
|
||||||
this.send = this.send.bind(this);
|
this.send = this.send.bind(this);
|
||||||
|
this.createAndConnect = this.createAndConnect.bind(this);
|
||||||
|
this.scheduleReconnect = this.scheduleReconnect.bind(this);
|
||||||
|
|
||||||
|
this.createAndConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
createAndConnect() {
|
||||||
const ws = new WebSocket(URL_WEBSOCKET);
|
const ws = new WebSocket(URL_WEBSOCKET);
|
||||||
ws.onopen = this.onOpen.bind(this);
|
ws.onopen = this.onOpen.bind(this);
|
||||||
ws.onclose = this.onClose.bind(this);
|
ws.onclose = this.onClose.bind(this);
|
||||||
@ -50,14 +56,15 @@ export default class Websocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Interface with other components
|
// Interface with other components
|
||||||
|
|
||||||
// Outbound: Other components can pass an object to `send`.
|
// Outbound: Other components can pass an object to `send`.
|
||||||
send(message) {
|
send(message) {
|
||||||
// Sanity check that what we're sending is a valid type.
|
// Sanity check that what we're sending is a valid type.
|
||||||
if (!message.type || !SOCKET_MESSAGE_TYPES[message.type]) {
|
if (!message.type || !SOCKET_MESSAGE_TYPES[message.type]) {
|
||||||
console.warn(`Outbound message: Unknown socket message type: "${message.type}" sent.`);
|
console.warn(
|
||||||
|
`Outbound message: Unknown socket message type: "${message.type}" sent.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageJSON = JSON.stringify(message);
|
const messageJSON = JSON.stringify(message);
|
||||||
@ -80,7 +87,6 @@ export default class Websocket {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
notifyRawMessageListeners(message) {
|
notifyRawMessageListeners(message) {
|
||||||
this.rawMessageListeners.forEach(function (callback) {
|
this.rawMessageListeners.forEach(function (callback) {
|
||||||
callback(message);
|
callback(message);
|
||||||
@ -102,13 +108,21 @@ export default class Websocket {
|
|||||||
this.websocket = null;
|
this.websocket = null;
|
||||||
this.notifyWebsocketDisconnectedListeners();
|
this.notifyWebsocketDisconnectedListeners();
|
||||||
this.handleNetworkingError('Websocket closed.');
|
this.handleNetworkingError('Websocket closed.');
|
||||||
this.websocketReconnectTimer = setTimeout(this.setupWebsocket, TIMER_WEBSOCKET_RECONNECT);
|
this.scheduleReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
// On ws error just close the socket and let it re-connect again for now.
|
// On ws error just close the socket and let it re-connect again for now.
|
||||||
onError(e) {
|
onError(e) {
|
||||||
this.handleNetworkingError(`Socket error: ${JSON.parse(e)}`);
|
this.handleNetworkingError(`Socket error: ${JSON.parse(e)}`);
|
||||||
this.websocket.close();
|
this.websocket.close();
|
||||||
|
this.scheduleReconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleReconnect() {
|
||||||
|
this.websocketReconnectTimer = setTimeout(
|
||||||
|
this.createAndConnect,
|
||||||
|
TIMER_WEBSOCKET_RECONNECT
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -120,7 +134,7 @@ export default class Websocket {
|
|||||||
try {
|
try {
|
||||||
var model = JSON.parse(e.data);
|
var model = JSON.parse(e.data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send PONGs
|
// Send PONGs
|
||||||
@ -140,6 +154,6 @@ export default class Websocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleNetworkingError(error) {
|
handleNetworkingError(error) {
|
||||||
console.error(`Websocket Error: ${error}`)
|
console.error(`Websocket Error: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user