问题描述
我正在尝试让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文件位于何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!