Problem to implement socket handler Angular Typescript

Viewed 107

I am trying to implement multiplayer scrabble and to handle socket (client side). I implemented a module but II dont know how to resolve this issue :

import * as io from 'socket.io-client';

export module SocketHandler {

  let activeSocket: SocketIOClient.Socket;

  export function requestSocket(server: string): any {

    if (activeSocket === undefined) {
        activeSocket = io.connect(server);
    }
    return activeSocket;
  }

  export function disconnectSocket(): void {
    activeSocket.disconnect();
    activeSocket = undefined;
  }

}

Error: export 'connect' (imported as 'io') was not found in 'socket.io-client' (possible exports: Manager, Socket, default, io) error TS2503: Cannot find namespace 'SocketIOClient'.

Can you please help me?

2 Answers

You need to use the prefix io before everything because you imported it with this name. Use directly io to connect.

activeSocket: io.Socket;

requestSocket(server: string): any {
  if (this.activeSocket === undefined) {
      this.activeSocket = io.io(server);
  }
  return this.activeSocket;
}

disconnectSocket(): void {
  this.activeSocket.disconnect();
  this.activeSocket = undefined;
}

Can you try use :

import * as socketIO from 'socket.io'
Related