mirror of
https://gitlab.com/futo-org/fcast.git
synced 2025-07-19 22:46:59 +00:00
Fixed websocket connections
This commit is contained in:
parent
bc06aadf9b
commit
211efbd385
1 changed files with 2 additions and 20 deletions
|
@ -6,7 +6,6 @@ import { v4 as uuidv4 } from 'modules/uuid';
|
||||||
|
|
||||||
export class WebSocketListenerService {
|
export class WebSocketListenerService {
|
||||||
public static PORT = 46898;
|
public static PORT = 46898;
|
||||||
private static TIMEOUT = 2500;
|
|
||||||
|
|
||||||
emitter = new EventEmitter();
|
emitter = new EventEmitter();
|
||||||
|
|
||||||
|
@ -57,22 +56,6 @@ export class WebSocketListenerService {
|
||||||
this.sessions.push(session);
|
this.sessions.push(session);
|
||||||
|
|
||||||
const connectionId = uuidv4();
|
const connectionId = uuidv4();
|
||||||
let heartbeatRetries = 0;
|
|
||||||
socket.setTimeout(WebSocketListenerService.TIMEOUT);
|
|
||||||
socket.on('timeout', () => {
|
|
||||||
try {
|
|
||||||
if (heartbeatRetries > 3) {
|
|
||||||
Main.logger.warn(`Could not ping device ${socket.remoteAddress}:${socket.remotePort}. Disconnecting...`);
|
|
||||||
socket.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
heartbeatRetries += 1;
|
|
||||||
session.send(Opcode.Ping);
|
|
||||||
} catch (e) {
|
|
||||||
Main.logger.warn(`Error while pinging sender device ${socket.remoteAddress}:${socket.remotePort}.`, e);
|
|
||||||
socket.destroy();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on("error", (err) => {
|
socket.on("error", (err) => {
|
||||||
Main.logger.warn(`Error.`, err);
|
Main.logger.warn(`Error.`, err);
|
||||||
|
@ -81,7 +64,6 @@ export class WebSocketListenerService {
|
||||||
|
|
||||||
socket.on('message', data => {
|
socket.on('message', data => {
|
||||||
try {
|
try {
|
||||||
heartbeatRetries = 0;
|
|
||||||
if (data instanceof Buffer) {
|
if (data instanceof Buffer) {
|
||||||
session.processBytes(data);
|
session.processBytes(data);
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,11 +82,11 @@ export class WebSocketListenerService {
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
this.sessions.splice(index, 1);
|
this.sessions.splice(index, 1);
|
||||||
}
|
}
|
||||||
this.emitter.emit('disconnect', { id: connectionId, type: 'ws', data: { url: socket.url() }});
|
this.emitter.emit('disconnect', { id: connectionId, type: 'ws', data: { url: socket.url }});
|
||||||
this.emitter.removeListener('ping', pingListener);
|
this.emitter.removeListener('ping', pingListener);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.emitter.emit('connect', { id: connectionId, type: 'ws', data: { url: socket.url() }});
|
this.emitter.emit('connect', { id: connectionId, type: 'ws', data: { url: socket.url }});
|
||||||
const pingListener = (message: any) => {
|
const pingListener = (message: any) => {
|
||||||
if (!message) {
|
if (!message) {
|
||||||
this.emitter.emit('ping', { id: connectionId });
|
this.emitter.emit('ping', { id: connectionId });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue