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

create socket instance without injection

Open weihangChen opened this issue 4 years ago • 2 comments

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

weihangChen avatar Jun 14 '21 14:06 weihangChen

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

T-450 avatar Jun 25 '21 05:06 T-450