javascript 使基本的 socket.io 示例工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9446708/
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
getting the basic socket.io sample to work
提问by user1111929
I'm having trouble even getting the very basic socket.io sample to run. For example the first example on the welcome page of their site:
我什至无法运行非常基本的 socket.io 示例。例如他们网站欢迎页面上的第一个例子:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
on the server side and
在服务器端和
<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>
on the client side. If I save the server-side in a host.js
file, and the client-side in a client.htm
file, and I run npm host.js
, I get
在客户端。如果我将服务器端保存在一个host.js
文件中,并将客户端保存在一个client.htm
文件中,然后我运行npm host.js
,我得到
info - socket.io started
warn - error raised: Error: listen EADDRINUSE
which is already not really expected. Then, for the client.htm
(or at least that's what I think that I'm supposed to do with it -- pasting it in a client.htm file), I only get a blank screen. Not very surprising, since it starts by including a nonexisting file /socket.io/socket.io.js
, but even changing this to host.js
(which I assume it is supposed to be) doesn't change the fact that I only get a blank screen...
这已经不是真正的预期了。然后,对于client.htm
(或者至少这是我认为我应该用它做的事情——将它粘贴到 client.htm 文件中),我只得到一个空白屏幕。并不奇怪,因为它首先包含一个不存在的文件/socket.io/socket.io.js
,但即使将其更改为host.js
(我认为应该是)也不会改变我只得到一个空白屏幕的事实......
I'm clueless.
我一窍不通
采纳答案by bryanmac
EADDRINUSE means that address is already in use so it can't get the socket. Is something else already running on port 80 on your machine? 80 is commonly used by web servers.
EADDRINUSE 表示该地址已在使用中,因此无法获取套接字。您机器的 80 端口上是否已经运行了其他东西?Web 服务器通常使用 80。
You can also try it on some other port.
您也可以在其他端口上尝试。
The reason you see a blank file is it doesn't connect to the node server (since it couldn't get the socket) so the on news event will never get called. It might even connecting to the socket of whatever else is running on 80 which will never emit that event :)
您看到空白文件的原因是它没有连接到节点服务器(因为它无法获取套接字),因此永远不会调用新闻事件。它甚至可能连接到 80 上运行的其他任何东西的套接字,它永远不会发出该事件:)
After you solve the port conflict, when you run the server, it should just say:
解决了端口冲突后,当你运行服务器时,它应该只是说:
info - socket.io started
信息 - socket.io 已启动
and now it's waiting for connections.
现在它正在等待连接。
Make sure you update the htm line to your port. For example, if 81:
确保将 htm 行更新到您的端口。例如,如果 81:
var socket = io.connect('http://localhost:81'); // note the :81
EDIT: I just tried it out and in the htm file I had to set the relative path to the socket.io.js file. After installing it via npm it was here in my directory.
编辑:我刚刚试了一下,在 htm 文件中我必须设置 socket.io.js 文件的相对路径。通过 npm 安装后,它在我的目录中。
<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
Make sure that path is relative to the htm file (doesn't start with a /). Here's how I found out where mine was:
确保路径相对于 htm 文件(不以 / 开头)。以下是我如何找到我的位置:
find . -name 'socket.io.js'
On Win: dir socket.io.js /s
在 Win: dir socket.io.js /s
You should also run the host with (on *nix you may need sudo in front):
您还应该使用以下命令运行主机(在 *nix 上,您可能需要在前面使用 sudo):
node host.js
Last thing I did when trying the sample was changing a couple lines in the htm file to this so I could see an alert message box when the event happened:
我在尝试示例时所做的最后一件事是将 htm 文件中的几行更改为此,以便在事件发生时我可以看到一个警报消息框:
socket.on('news', function (data) {
alert(JSON.stringify(data));