让基本的socket.io示例工作 [英] getting the basic socket.io sample to work

查看:98
本文介绍了让基本的socket.io示例工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我甚至无法运行非常基本的socket.io示例。例如,他们网站欢迎页面上的第一个例子:

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:

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);
  });
});

服务器端和

<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>

。如果我将服务器端保存在 host.js 文件中,而客户端保存在 client.htm 中文件,我运行 npm host.js ,我得到

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

   info  - socket.io started
   warn  - error raised: Error: listen EADDRINUSE

已经是没想到。然后,对于 client.htm (或者至少我认为我应该用它做什么 - 将它粘贴在client.htm文件中),我只得到一个空白的屏幕。这并不奇怪,因为它首先包含一个不存在的文件 /socket.io/socket.io.js ,但即使将其更改为 host.js (我认为它应该是这样)并没有改变我只得到一个空白屏幕的事实...

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...

我' m clueless。

I'm clueless.

推荐答案

EADDRINUSE表示该地址已被使用,因此无法获取套接字。机器上的端口80上是否还有其他东西在运行?网络服务器通常使用80.

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.

您也可以在其他端口上尝试。

You can also try it on some other port.

原因你看到一个空白文件是它没有连接到节点服务器(因为它无法获得套接字)所以on news事件永远不会被调用。它甚至可能连接到80上运行的其他任何东西的套接字,它永远不会发出该事件:)

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 :)

解决端口冲突后,当你运行服务器时,它应该说:

After you solve the port conflict, when you run the server, it should just say:


info - socket.io started

info - socket.io started

现在它正在等待连接。

确保将htm行更新到您的端口。例如,如果81:

Make sure you update the htm line to your port. For example, if 81:

var socket = io.connect('http://localhost:81'); // note the :81

编辑:
我刚试了一下,在htm中文件我必须设置socket.io.js文件的相对路径。通过npm安装后,它就在我的目录中。

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.

<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>

确保路径相对于htm文件(不以/开头)。以下是我如何找到我的位置:

Make sure that path is relative to the htm file (doesn't start with a /). Here's how I found out where mine was:

find . -name 'socket.io.js'

On Win:
dir socket.io。 js / s

On Win: dir socket.io.js /s

您还应该运行主机(在* nix上,您可能需要在前面使用sudo):

You should also run the host with (on *nix you may need sudo in front):

node host.js

我在尝试时做的最后一件事该示例正在将htm文件中的几行更改为此,以便在事件发生时我看到一个警告消息框:

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:

socket.on('news', function (data) {
   alert(JSON.stringify(data));

这篇关于让基本的socket.io示例工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆