ng-socket-io
ng-socket-io copied to clipboard
Change url in component
Hey guys,
I want to implement a client application where a user can provide a server url. So the socket url needs to be dynamically set.
At the moment I have to hardcode the url to app.module.ts:
import { SocketIoModule, SocketIoConfig } from 'ng-socket-io';
const config: SocketIoConfig = { url: 'http://xxx.xxx.x.x:PORT' };
but I need something like this (serverUrl is an input in home.html. connectToServer() is called on a button click):
export class HomePage {
serverUrl: '';
constructor(public navCtrl: NavController, private socket: Socket) {}
connectToServer() {
this.socket.connect(this.serverUrl);
if (this.socket.ioSocket.connected) {
this.navCtrl.push('DeckPage');
}
}
}
Can you guys provide any help? Thanks in Advance!
Best regards Nick
Hello,
I also need to make the URL configurable, have you found a solution for this situation?
@Injectable() export class Socket_Test extends Socket {
constructor() {
super({ url: '<SOCKET_URL>', options: {"rememberUpgrade":true,
"transports":["websocket","polling"],
"secure":true,"rejectUnauthorized":true,
"autoConnect":false,
"query":"token=<TOKEN VALUE>"
} });
}
init(token){
this.ioSocket.query = {token:token};
}
}
call function this.socket.init(<TOKEN VALUE>); this.socket.connect();