简体   繁体   English

在多台机器上的socket.io

[英]socket.io on multiple machines

I've two linux-boxes and a mobile client with a stock-browser and want to achieve this: 我有两个linux-boxes和一个带有股票浏览器的移动客户端,想要实现这一目标:

linux-box-1 (master) creates http-server, servers http-content and nodejs with socket.io by linux-box-1(master)通过socket.io创建http服务器,服务器http-content和nodejs

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var i = 1;
app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
});

The mobile-client is now able to connect to this server and right now it fires one event when a button is hit: 现在,移动客户端可以连接到该服务器,并且当按下按钮时,它现在会触发一个事件:

<html>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://192.168.0.109/');
  socket.on('news', function (data) {
    console.log(data);
  });
</script>
<button type="button" onclick="socket.emit('foto', { my: 'data' });">Click Me!</button>
</body>
</html>

Now, my problem is that I want the second linux-box to run nodejs and socket.io and is able to recieve this event I emit. 现在,我的问题是我希望第二个linux-box运行nodejs和socket.io并能够接收我发出的此事件。 I have absolutely no clue how this code should look as I did not find any example for this kind of application. 我完全不知道这段代码的外观,因为我没有找到这种应用程序的任何示例。

Can somebody help? 有人可以帮忙吗?

Thanks!!! 谢谢!!!

You could create another socket on the client which connects to the second server. 您可以在连接到第二台服务器的客户端上创建另一个套接字。 Then instead of emitting an event on onClick to only one socket you could call a function that emits this event to both sockets. 然后,您可以调用一个将事件发送到两个套接字的函数,而不是只在一个套接字上触发onClick事件。

If have written the example below which is based on you example. 如果写了下面的示例,该示例基于您的示例。 But in contrast to your environment it simulates the two linux-boxes by running the two apps both on the same server but on different ports. 但是与您的环境相反,它通过在同一服务器上但在不同端口上运行两个应用程序来模拟两个linux-box。

code for the servers: 服务器代码:

//servers.js
//run with: node servers.js
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var i = 1;
app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on ('foto', function (data) {
    console.log ('server1');
    console.log (data);
  });
});

var app2 = require('http').createServer(handler)
io2 = require('socket.io')(app2);
app2.listen (8081);

io2.on('connection', function (socket) {
  socket.on ('foto', function (data) {
    console.log ('server2');and connect
    console.log (data);
  });
});

code for the client in index.html: 客户端中index.html的代码:

<html>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/');
  var socket2 = io.connect('http://localhost:8081/');
  socket.on('news', function (data) {
    console.log(data);
  });
  function myclick () {
    console.log("click");
    socket.emit('foto', { my: 'data' });
    socket2.emit('foto', { my: 'data' });

  }
</script>
<button type="button" onclick="myclick();">Click Me!</button>
</body>
</html>

Use your browser and navigate to http://localhost:8080 or http://localhost:8081 . 使用浏览器并导航到http://localhost:8080http://localhost:8081 Both should work since they both run the same application. 两者都应该运行,因为它们都运行相同的应用程序。 When you click on the button both servers will log the data to the command line. 当您单击按钮时,两个服务器都将数据记录到命令行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM