Connecting client to server using Socket.io

前端 未结 2 443
情深已故
情深已故 2021-01-29 22:18

I\'m relatively new to node.js and it\'s addons, so this is probably a beginnersquestion.

I\'m trying to get a simple HTML page on a webserver connect to a different ser

相关标签:
2条回答
  • 2021-01-29 22:23

    You need to make sure that you add forward slash before your link to socket.io:

    <script src="/socket.io/socket.io.js"></script>
    

    Then in the view/controller just do:

    var socket = io.connect()
    

    That should solve your problem.

    0 讨论(0)
  • 2021-01-29 22:44

    Have you tried loading the socket.io script not from a relative URL?

    You're using:

    <script src="socket.io/socket.io.js"></script>
    

    And:

    socket.connect('http://127.0.0.1:8080');
    

    You should try:

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    

    And:

    socket.connect('http://localhost:8080');
    

    Switch localhost:8080 with whatever fits your current setup.

    Also, depending on your setup, you may have some issues communicating to the server when loading the client page from a different domain (same-origin policy). This can be overcome in different ways (outside of the scope of this answer, google/SO it).

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