ngx-socket-io
ngx-socket-io copied to clipboard
Capture a error when the server is down
I need to capture and hide this error, when the server is down.
websocket.js:88
WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=4&transport=websocket' failed:
| doOpen | @ | websocket.js:88
| open | @ | transport.js:44
| open | @ | socket.js:194
| Socket | @ | socket.js:119
| push.4+Ds.module.exports | @ | index.js:3
| open | @ | manager.js:112
| Manager | @ | manager.js:41
| lookup | @ | index.js:38
| WrappedSocket | @ | socket-io.service.ts:24
| SocketFactory | @ | socket-io.module.ts:7
| factory | @ | core.js:11443
| hydrate | @ | core.js:11354
| get | @ | core.js:11175
| injectInjectorOnly | @ | core.js:899
| ɵɵinject | @ | core.js:903
| SocketProviderConnect_Factory | @ | SocketProviderConnect.ts:19
| hydrate | @ | core.js:11354
| get | @ | core.js:11175
| get | @ | core.js:24315
| get | @ | core.js:24012
| getOrCreateInjectable | @ | core.js:4169
| ɵɵdirectiveInject | @ | core.js:14716
| DebugView_Factory | @ | debug-view.ts:14
| getNodeInjectable | @ | core.js:4274
| instantiateRootComponent | @ | core.js:8026
| createRootComponent | @ | core.js:13542
| create | @ | core.js:24101
| createComponent | @ | core.js:10205
| activateWith | @ | router_outlet.ts:165
| activateRoutes | @ | activate_routes.ts:188
This is my config and code, I couldn't hide the error Maybe I should post this issue in socket.io Sorry for the inconvenience
const socketIoConfig: SocketIoConfig = {
url: 'http://localhost:3000',
options: {
transports: ['websocket'],
reconnection: false
}
};
...
SocketIoModule.forRoot(socketIoConfig),
...
constructor(public socket: Socket) {
const connect = this.socket.connect();
console.log(connect)
if (connect.connected) {
this.socket.ioSocket.on('message', (res) => {
console.log("message: ", res)
this.publicMessageEvent.emit(res)
})
this.socket.ioSocket.on(this.socket.ioSocket.id, (res) => {
console.log("private: ", res)
this.privateMessageEvent.emit(res)
})
}
this.socket.ioSocket.on('connect_error', err => SocketProviderConnect.handleErrors(err));
this.socket.ioSocket.on('connect_failed', err => SocketProviderConnect.handleErrors(err));
this.socket.ioSocket.on('disconnect', err => SocketProviderConnect.handleErrors(err));
this.socket.ioSocket.on('error', err => SocketProviderConnect.handleErrors(err));
}
I've got the same problem with the NestJS WebSocket gateway on server restart (all API gateways get stuck in the restart process until the last opened client is closed OR use the disconnect() method on the socket - but I need to catch this first...)
the only thing i found was this in my Angular app
public connection;
constructor(private socket: Socket) { this.connection = chatService.connect(); }
this in my template (though i haven't managed to Type the return it seems to work) *ngIf="!connection.connected"