使用 TypeScript 的 socket.io 客户端

发布于 2025-01-11 02:12:21 字数 891 浏览 0 评论 0原文

我尝试在客户端中将 TypeScript 与 socket.io 一起使用:

import { Manager, Socket} from "socket.io-client";
import {DefaultEventsMap} from "@socket.io/component-emitter";

export class SocketIoService {
    private socket: Socket<DefaultEventsMap, DefaultEventsMap>

    constructor() {
        const manager = new Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }

    public connect() {
        this.socket.emit("hello");
        this.socket.on("noArg", () => {
            // ...
        });

        this.socket.on("basicEmit", (a, b, c) => {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    }
}

问题是当我启动服务器时,出现以下错误:

The requested module './../../../parseuri/index.js' does not provide an export named 'default'

我做错了什么?

I am trying to use TypeScript with socket.io in my client:

import { Manager, Socket} from "socket.io-client";
import {DefaultEventsMap} from "@socket.io/component-emitter";

export class SocketIoService {
    private socket: Socket<DefaultEventsMap, DefaultEventsMap>

    constructor() {
        const manager = new Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }

    public connect() {
        this.socket.emit("hello");
        this.socket.on("noArg", () => {
            // ...
        });

        this.socket.on("basicEmit", (a, b, c) => {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    }
}

The problem is when I start my server I get the following error:

The requested module './../../../parseuri/index.js' does not provide an export named 'default'

What have I done wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱你一生 2025-01-18 02:12:21

服务器:

import { Server } from "socket.io";

const io = new Server(httpServer);

客户端:

import { Manager } from "socket.io-client";

const manager = new Manager("http://localhost:3000");
const socket = manager.socket("/");

Server:

import { Server } from "socket.io";

const io = new Server(httpServer);

Client:

import { Manager } from "socket.io-client";

const manager = new Manager("http://localhost:3000");
const socket = manager.socket("/");
递刀给你 2025-01-18 02:12:21

(这应该是一条评论,但我没有足够的代表来发表评论)

该错误表示您正在尝试从已命名导出的模块默认导入

你能附上错误的堆栈跟踪吗?

我对触发导入“./../../../parseuri/index.js”的模块感兴趣。

(this should have been a comment but I do not have enough rep to comment)

The error signifies that you are trying to default import from a module which has named exports.

Can you attach the stacktrace of the error?

I am interested in the module which triggers the import to './../../../parseuri/index.js'.

幻想少年梦 2025-01-18 02:12:21

我不完全确定该错误与此特定代码有何关系。我刚刚创建了一个空文件夹,添加了您的代码,并且使用空的 tsconfig.json 就能够编译。这是编译后的代码:

"use strict";
exports.__esModule = true;
exports.SocketIoService = void 0;
var socket_io_client_1 = require("socket.io-client");
var SocketIoService = /** @class */ (function () {
    function SocketIoService() {
        var manager = new socket_io_client_1.Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }
    SocketIoService.prototype.connect = function () {
        this.socket.emit("hello");
        this.socket.on("noArg", function () {
            // ...
        });
        this.socket.on("basicEmit", function (a, b, c) {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    };
    return SocketIoService;
}());
exports.SocketIoService = SocketIoService;

我建议这样做:

$ rm -rf node_modules
$ npm i

然后再试一次。

I'm not entirely sure how the error is related to this specific code. I just created an empty folder, added your code in, and with an empty tsconfig.json was able to compile. Here's the compiled code:

"use strict";
exports.__esModule = true;
exports.SocketIoService = void 0;
var socket_io_client_1 = require("socket.io-client");
var SocketIoService = /** @class */ (function () {
    function SocketIoService() {
        var manager = new socket_io_client_1.Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }
    SocketIoService.prototype.connect = function () {
        this.socket.emit("hello");
        this.socket.on("noArg", function () {
            // ...
        });
        this.socket.on("basicEmit", function (a, b, c) {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    };
    return SocketIoService;
}());
exports.SocketIoService = SocketIoService;

I'd suggest doing:

$ rm -rf node_modules
$ npm i

And trying again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文