Socket.IO 客户端 .js 文件在哪里? [英] Where is the Socket.IO client-side .js file located?
问题描述
我正在尝试让 socket.io(节点库)工作.
I am trying to get socket.io (Node library) to work.
我的服务器端 js 正在运行,它正在监听.socket.io 网站简单说明:
I have the server-side js working, and it is listening. The socket.io website states simply:
<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>
这很好,但是,我要导入什么 JS 文件!?!
This is nice, however, what JS file am I importing!?!
进入node_modules目录,通过npm安装socket.io,socket.io/lib/
里面是socket.io.js
文件.但是,这是服务器端的(使用短语 require()
,这会在客户端出错).
I went into the node_modules directory, where I installed socket.io through npm, and inside socket.io/lib/
is socket.io.js
file. However, this is server-side (uses the phrase require()
, which errors on the client).
我花了一个小时环顾四周,但我无法让任何客户端 .js 文件工作.
I have spent an hour looking around and I can't get any client .js file to work.
我错过了什么?
推荐答案
我最终设法自己回答了这个问题.
I managed to eventually answer this for myself.
socket.io 入门页面在这方面不清楚,但我发现 socket.io 的服务器端自动将 .js 文件托管在起始节点上,在文档中指定的目录中:
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"
因此,无论您的网络应用程序结构如何,您都只需指向此网址,它就可以工作.
So you literally just point to this url regardless of your web app structure, and it works.
这篇关于Socket.IO 客户端 .js 文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!