javascript Socket.IO 客户端 .js 文件在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17757728/
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-27 09:30:10  来源:igfitidea点击:

Where is the Socket.IO client-side .js file located?

javascriptnode.jssocket.io

提问by dthree

I am trying to get socket.io (Node library) to work.

我正在尝试让 socket.io(节点库)工作。

I have the server-side js working, and it is listening. The socket.io website states simply:

我有服务器端 js 工作,它正在侦听。socket.io 网站简单地说明:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

This is nice, however, what JS file am I importing!?!

这很好,但是,我要导入什么 JS 文件!?!

I went into the node_modules directory, where I installed socket.io through npm, and inside socket.io/lib/is socket.io.jsfile. However, this is server-side (uses the phrase require(), which errors on the client).

进入node_modules目录,通过npm安装socket.io,里面socket.io/lib/socket.io.jsfile。但是,这是服务器端的(使用短语require(), which errors on the client)。

I have spent an hour looking around and I can't get any client .js file to work.

我花了一个小时环顾四周,但无法让任何客户端 .js 文件工作。

What am I missing?

我错过了什么?

回答by dthree

I managed to eventually answer this for myself.

我最终设法为自己回答了这个问题。

The socket.io getting started page isn't clear on this, but I found that the server side of socket.io automatically hosts the .js file on starting node, in the directory specified in the documentation:

socket.io 入门页面对此并不清楚,但我发现 socket.io 的服务器端自动将 .js 文件托管在起始节点上,在文档中指定的目录中:

"/socket.io/socket.io.js"

So you literally just point to this url regardless of your web app structure, and it works.

因此,无论您的 Web 应用程序结构如何,您都只需指向此 url,它就可以工作。

回答by Shrey Gupta

I would suggest checking if your node_modules directory is at the top level of your app directory. Also, I do believe you need to specify a port number; you should write something like var socket = io.connect('http://localhost:1337');, where the port number is 1337.

我建议检查您的 node_modules 目录是否位于应用程序目录的顶层。另外,我相信您需要指定一个端口号;你应该写一些类似的东西var socket = io.connect('http://localhost:1337');,端口号在哪里1337

回答by OrangeDog

The client is available in a few ways:

客户端可通过以下几种方式使用:

  • supplied by the socket.io server at /socket.io/socket.io.js
  • included via webpack as the module socket.io-client
  • via the official CDN https://cdnjs.cloudflare.com/ajax/libs/socket.io/<version>/socket.io.js
  • 由 socket.io 服务器提供 /socket.io/socket.io.js
  • 通过 webpack 作为模块包含 socket.io-client
  • 通过官方 CDN https://cdnjs.cloudflare.com/ajax/libs/socket.io/<version>/socket.io.js

For the first one, the server can be configured in a couple of ways:

对于第一个,可以通过两种方式配置服务器:

// standalone
var io = require('socket.io')(port);

// with existing server from e.g. http.createServer or app.listen
var io = require('socket.io')(server);

回答by refactor

Hereyou can find CDN for various versions.

在这里您可以找到各种版本的 CDN。

回答by Anshul Bisht

If you did npm installthen the client socket.io file is located at node_modules/socket.io-client/dist/socket.io.js

如果您安装npm,那么客户端 socket.io 文件位于node_modules/socket.io-client/dist/socket.io.js