1

我的目标是让 socket.io 与 deno 一起工作。Deno 确实有一个用于 Web 套接字的标准库,但它没有故障转移。我认为可以使用 UNPKG 服务在你的 deno 程序中使用 NPM 包,但我似乎在导入 socket.io 时语法错误:

import { serve } from "https://deno.land/std/http/server.ts";
import {Socket} from "https://unpkg.com/browse/socket.io@3.0.1/dist/index.d.ts";

new Worker(new URL("worker.js", import.meta.url).href, { type: "module" });
const server = serve({ port: 3001 });
const app = new Application();
const io = Socket(3001);

// serve index page
if (req.url === "/") {
  req.respond({
    status: 200,
    body: await Deno.open("./public/index.html"),
  });
}

io.on("connection", (socket) => {
  // either with send()
  socket.send("Hello!");

  // or with emit() and custom event names
  socket.emit("greetings", "Hey!", { "ms": "jane" }, Buffer.from([4, 3, 3, 1]));

  // handle the event sent with socket.send()
  socket.on("message", (data) => {
    console.log(data);
  });

  // handle the event sent with socket.emit()
  socket.on("salutations", (elem1, elem2, elem3) => {
    console.log(elem1, elem2, elem3);
  });
});

我收到以下错误:

error: An unsupported media type was attempted to be imported as a module.
  Specifier: https://unpkg.com/browse/socket.io@3.0.1/dist/index.d.ts
  MediaType: Unknown
4

2 回答 2

1

尝试这个:

import Socket from 'https://cdn.esm.sh/v9/socket.io@3.0.3/esnext/socket.io.js';

正如@Indecisive 所说,you are importing a .d.ts file.

您可以使用@Marcos Casagrande:https ://stackoverflow.com/a/61821141/6250402 (记住socket.io使用 npm 安装模块)

或使用https://deno.land/std@0.79.0/ws/mod.ts

于 2020-12-07T02:51:00.950 回答
1

它失败是因为您正在导入一个.d.ts文件,该文件是一个声明文件,本质上您无法从中运行任何代码 - 它纯粹是为了支持类型。

相反,您应该替换index.d.tsindex.js

于 2020-11-29T16:43:21.907 回答