I've two linux-boxes and a mobile client with a stock-browser and want to achieve this:
linux-box-1 (master) creates http-server, servers http-content and nodejs with socket.io by
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. 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.
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.
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:
<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
. 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.