/********************** CODICE **************************/
Ecco ciò che ho realizzato (in versione semplificata) fin'ora: il server e il webclient. Manca il client QT.
codice:
<!-- client.js
Il WebClient è composto da due bottoni, #Bt1 e #Btn2. Cliccando uno dei due bottoni si inverà al server il valore 1 o 2.
Il server si occuperà di di inviare a tutti i client che lo stamperanno in #state.
-->
<html>
<head>
<title>WebClient</title>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>
<form action="" name="f">
<button id="btn1" type="button" onClick="submitFunction(1)">Btn1</button>
<button id="btn2" type="button" onClick="submitFunction(2)">Btn2</button>
<span id="state">STATE (0)</span>
</form>
<SCRIPT>
var socket = new io.connect('localhost:3000');
function submitFunction(i) {
if (i==1){
console.log(i);
socket.emit('pState', i);
}
if (i==2){
console.log(i);
socket.emit('pState', i);
}
}
socket.on('pState', function(msg){
if (msg==1){
$('#state').text("STATE (" + msg + ")");
}
if (msg==2){
$('#state').text("STATE (" + msg + ")");
}
});
</SCRIPT>
</body>
</html>
codice:
// server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/client.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
// il server riceve il valore 1/2 e lo rimanda ai client
io.on('connection', function(socket){
socket.on('pState', function(msg){
console.log('STATE: ' + msg);
io.emit('pState', msg);
});
});