[英]socket.io client with TypeScript
我正在嘗試在我的客戶端中使用 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'
我做錯了什么?
服務器:
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("/");
(這應該是評論,但我沒有足夠的代表發表評論)
該錯誤表示您正在嘗試從名為 exports 的模塊中default import
。
你能附上錯誤的堆棧跟蹤嗎?
我對觸發導入“./../../../parseuri/index.js”的模塊感興趣。
我不完全確定該錯誤與此特定代碼有何關系。 我剛剛創建了一個空文件夾,在其中添加了您的代碼,並且使用一個空的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
並再次嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.