Home > other >  Problem to implement socket handler Angular Typescript
Problem to implement socket handler Angular Typescript

Time:10-12

am trying to implement multiplayer scrabble and to handle socket (client side) i implemented a modul but i dont know how to resolve those 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'.

6 let activeSocket: SocketIOClient.Socket;

                   can you please help me , thanks

enter image description here

CodePudding user response:

Can you try use :

import * as socketIO from 'socket.io'

CodePudding user response:

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;
}
  • Related