admin管理员组

文章数量:1220947

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

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

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

I'm clueless.

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

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

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

I'm clueless.

Share Improve this question edited Aug 21, 2012 at 11:20 bryanmac 39.3k10 gold badges92 silver badges99 bronze badges asked Feb 25, 2012 at 18:20 user1111929user1111929 6,09710 gold badges45 silver badges75 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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.

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

and now it's waiting for connections.

Make sure you update the htm line to your port. For example, if 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.

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

find . -name 'socket.io.js'

On Win: dir socket.io.js /s

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

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:

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

本文标签: javascriptgetting the basic socketio sample to workStack Overflow