ng-socket-io icon indicating copy to clipboard operation
ng-socket-io copied to clipboard

Change url in component

Open NickHatBoecker opened this issue 7 years ago • 2 comments

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

NickHatBoecker avatar Jun 07 '18 23:06 NickHatBoecker

Hello,

I also need to make the URL configurable, have you found a solution for this situation?

pierresh avatar Dec 31 '20 05:12 pierresh

@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();

zubinraja avatar Feb 16 '21 18:02 zubinraja