ngx-socket-io
ngx-socket-io copied to clipboard
create socket instance without injection
I have endpoints that are totally dynamic, and remain unknown for injection. How can I create a socket manually using like
let socketIoConfig: SocketIoConfig = {
url: "dynamicEndpoint:port",
options: {
autoConnect: false,
transports: ['websocket'],
setTimeout:10000
}
};
let s = new Socket(socketIoConfig);
import { Injectable } from '@angular/core';
import { Socket, SocketIoConfig } from './../../../node_modules/ngx-socket-io';
@Injectable({
providedIn: 'root',
})
export class SocketService {
config: SocketIoConfig = {
url: 'http://192.168.43.142:9780',
options: {},
};
constructor(private socket: Socket) {
this.socket = new Socket(this.config);
}
changeToAdminSocket() {
this.socket.disconnect();
this.config.url = 'http://192.168.43.142:9781'; // Note the port change
this.socket = new Socket(this.config);
}
}
https://stackoverflow.com/questions/60685483/angular-change-socket-io-url-on-runtime