当本地主机服务器提供实际页面时,如何使用 socket.io 与另一台服务器通信? [英] How to use socket.io to communicate with another server when the actual page is being served by a localhost server?

查看:10
本文介绍了当本地主机服务器提供实际页面时,如何使用 socket.io 与另一台服务器通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 localhost(XAMPP、Apache)为我的页面提供服务,在我朋友的物理服务器上,我运行一个 node.js 服务器,用于与页面(游戏)通信.

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

这是 node.js 服务器代码:

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

它运行时没有任何错误,但我不知道如何将 socket.io 代码包含到我的网页中!我该怎么做?

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?

推荐答案

在页面中包含脚本标签:

Include a script tag in your page:

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

它将由您的 node.js 服务器提供服务.

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

除此之外,您可以按照 socket.io 上的示例进行操作,例如:

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

这篇关于当本地主机服务器提供实际页面时,如何使用 socket.io 与另一台服务器通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆