getting the basic socket.io sample to work

前端 未结 1 1742
眼角桃花
眼角桃花 2021-02-07 09:26

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(\'soc         


        
1条回答
  •  误落风尘
    2021-02-07 09:49

    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.

    
    

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

    0 讨论(0)
提交回复
热议问题