Now that we have animations and our web pages ready, let's handle the emitted events in our server:
Edit your index.js
file by typing vi index.js
and paste the following code to the end of the file:
io.on('connection', function (socket) { socket.on('action', function (data) { if (data === 'play') io.sockets.emit('video', 'play'), if (data === 'stop') io.sockets.emit('video', 'stop'), if(data === 'next') { animation.stop(); // Broadcast to all connected sockets io.sockets.emit('video', 'next'), } }); socket.on('video', function (data) { if (data === 'started') { animation.start(); // Broadcast to all connected sockets io.sockets.emit('status', 'playing'), } if (data === 'stopped') { animation.stop(); // Broadcast to all connected sockets io.sockets.emit('status', 'stopped'), } }); });
Now we should be able to handle all the events and coordinate all the system. On receiving an event, we'll reply using io.sockets.emit()
, which allows us to emit an event to all the connected sockets.
On the top of the file, we also need to import the animations file to be able to use it by requiring it and assigning it to a var
:
var animation = require('./animations'),
Now we are ready to test the whole system. Start the server with the command node index.js
. On the same network as Galileo, open a browser in your computer with the URL http://my_ip_address:8080/player
and another one in your mobile phone with the URL http://my_ip_address:8080/controls
.
Using the control web page in your mobile phone, you'll be able to start, stop, or skip the animations along with the music videos.
3.15.6.77