How to use socket.io to communicate with another server when the actual page is being served by a localhost server?

后端 未结 2 840
逝去的感伤
逝去的感伤 2020-12-16 20:48

I\'m serving my page through localhost (XAMPP, Apache), and on my friend\'s physical server I run a node.js server that is used for communication with the page (a game).

相关标签:
2条回答
  • 2020-12-16 21:17

    Include a script tag in your page:

    <script src="http://[YOUR IP]:1235/socket.io/socket.io.js">
    

    And it will be served by your node.js server.

    Apart from that, you can just follow the examples on socket.io, e.g.:

    var socket = io.connect("http://[YOUR IP]:1235");
    
    socket.emit("start", "LET'S GO!");
    
    0 讨论(0)
  • 2020-12-16 21:18

    2 Options. Per the documentation, you can do a JavaScript src pointing at your node server:

    <script src="http://url.to.node.com/socket.io/socket.io.js"></script>
    

    Or you can include it manually, grabbing it from the Git repo at https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js

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