如何在带有 Visual Studio Code 的 TypeScript 中使用 Socket.IO?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34258935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:09:44  来源:igfitidea点击:

How to use Socket.IO in TypeScript with Visual Studio Code?

socket.iotypescriptvisual-studio-code

提问by Adam Harte

I am looking for a simple example of how to get Socket.IO working with TypeScript while using Visual Studio Code. I am running on Node.js.

我正在寻找一个简单的示例,说明如何在使用 Visual Studio Code 时让 Socket.IO 与 TypeScript 一起工作。我在 Node.js 上运行。

I have installed the d.ts for socket.io, and it appears in the typings folder.

我已经为 socket.io 安装了 d.ts,它出现在typings 文件夹中。

cd src/
tsd query socket.io --action install

I have added socket.io to the package.json dependencies.

我已将 socket.io 添加到 package.json 依赖项中。

"dependencies": {
    "socket.io" : "*"
}

The run npm install, and the package files are showing in the node_modulesdirectory.

runnpm install和包文件显示在node_modules目录中。

I then try to import socket.io in TypeScript, and here is where I don't know what I am doing. I tried this:

然后我尝试在 TypeScript 中导入 socket.io,在这里我不知道我在做什么。我试过这个:

import * as sio from 'socket.io';

It doesn't complain about anything, but when I just to use sioit does not give me any IntelliSense. So I feel like I have done something wrong.

它没有抱怨任何东西,但是当我只是使用sio它时,它并没有给我任何智能感知。所以我觉得我做错了什么。

Is that the correct import? Are there any up to dateexamples around?

那是正确的import吗?有没有最新的例子?

回答by basarat

import * as sio from 'socket.io';

import * as sio from 'socket.io';

That is the right import. You can see it in use here : https://github.com/TypeScriptBuilder/tsb/blob/ac87f359201a25e1bbb44f72093487348f3d28d3/src/socketLib/socketLibServer.ts#L2Try restarting VS code.

这是正确的进口。您可以在此处看到它的使用情况:https: //github.com/TypeScriptBuilder/tsb/blob/ac87f359201a25e1bbb44f72093487348f3d28d3/src/socketLib/socketLibServer.ts#L2尝试重新启动 VS 代码。

Are there any up to date examples around?

有没有最新的例子?

I use socket.io quite heavily here : https://github.com/TypeScriptBuilder/tsb

我用socket.io相当严重这里:https://github.com/TypeScriptBuilder/tsb