最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin11浏览0评论

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 munication with the page (a game).

This is the node.js server code:

var io = require('socket.io').listen(1235);

io.sockets.on('connection', function (socket)
{
    socket.on("start", function (data)
    {
        console.log(data);
    });
});

It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that?

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 munication with the page (a game).

This is the node.js server code:

var io = require('socket.io').listen(1235);

io.sockets.on('connection', function (socket)
{
    socket.on("start", function (data)
    {
        console.log(data);
    });
});

It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that?

Share Improve this question asked May 3, 2012 at 15:51 corazzacorazza 32.4k39 gold badges120 silver badges191 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 20

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

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论