【问题标题】:Problem to implement socket handler Angular Typescript实现套接字处理程序Angular Typescript的问题
【发布时间】:2021-11-27 03:26:48
【问题描述】:

我正在尝试实现多人拼字游戏并处理套接字(客户端)。我实现了一个模块,但我不知道如何解决这个问题:

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

}

错误:在“socket.io-client”中找不到导出“connect”(导入为“io”)(可能的导出:Manager、Socket、默认、io) 错误 TS2503:找不到命名空间“SocketIOClient”。

你能帮帮我吗?

【问题讨论】:

    标签: angular sockets client


    【解决方案1】:

    你可以尝试使用:

    import * as socketIO from 'socket.io'
    

    【讨论】:

    • 我已经尝试过,但工作量不大,这个模块对我的项目很重要,所以如果你能帮助我,我会收到这些错误:错误:src/app/modules/socketHandler.module.ts:1 :1 - 错误 TS6133: 'socketIO' 被声明但它的值从未被读取。 1 从'socket.io'导入*作为socketIO;错误:src/app/modules/socketHandler.module.ts:4:23 - 错误 TS2709:不能使用命名空间“SocketIO”作为类型。 4 让 activeSocket:SocketIO 错误:src/app/modules/socketHandler.module.ts:9:28 - 错误 TS2304:找不到名称 'io'。 9 activeSocket = io.connect(server);
    【解决方案2】:

    您需要在所有内容之前使用前缀io,因为您使用此名称导入了它。 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 2016-09-17
      • 2011-08-19
      • 2010-09-30
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      相关资源
      最近更新 更多