socket.io.js not found on client

后端 未结 2 1166
情书的邮戳
情书的邮戳 2021-01-23 03:10

This question has been asked previously but haven\'t found any solution in prior replies.

Socket.IO gives me two problems:

  1. server side gave this error - ER
相关标签:
2条回答
  • 2021-01-23 03:14

    The problem here is that you're telling Socket.IO to listen on a server that does not yet exist, causing an EACCES and therefore not serving the client file. This is what you're doing:

    // the HTTP server doesn't exist yet
    var io = require('socket.io').listen(server);
    var server = http.createServer();
    

    And if you saw in your server-side error console, you get this:

    info: socket.io started
    warn: error raised: Error: listen EACCES
    

    To fix this, move your listen function to after the server is created:

    var server = http.createServer();
    var io = require('socket.io').listen(server);
    

    Once Socket.IO listens properly, it will automatically serve the client file to /socket.io/socket.io.js. You don't need to find it or serve it manually.

    0 讨论(0)
  • 2021-01-23 03:27

    The client file that you need is located inside the node_modules folder here:

    node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
    

    Socket.io should serve this file though so you don't need to copy it somewhere else. For example if you are running your socket.io server on:

    http://localhost:5000
    

    Then the script will be served from:

    http://localhost:5000/socket.io/socket.io.js
    

    If you are consuming socket.io from another application or another port the relative URL in your code example won't work. You'll want to include the client-script manually or try including the client node module (if consumed by node app).

    You can view the client repository here for more info: https://github.com/LearnBoost/socket.io-client

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