server.js
var http = require('http'); var express = require('express'); //package che ci semplifica la parte server-side var app = express(); //this is different from old method .createServer(), it returns a javascript function var jade = require('jade'); //tamplate gestore var io = require('socket.io'); var server = http.createServer(app); io = io.listen(server); //this return the new object we want to listen to server.listen(3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set("view options", {layout: false}); app.use(express.static(__dirname + '/public')); //no need to use .configure app.get('/', function(req, res){ res.render('home.jade'); }); //app.listen(3000); //express ascolterà questa porta //connection even lanciato quando un client prova a collegarsi al server //socket.io crea una nuova socket (utilizzata poi per scambiare messaggi) io.sockets.on('connection', function(socket){ socket.on('setPseudo', function(data){ socket.set('pseudo', data); //il nickname? lo pseudonimo per quella socket? }); socket.on('message', function (message) { socket.get('pseudo', function (error, name) { var data = { 'message' : message, pseudo : name }; socket.broadcast.emit('message', data); console.log("user " + name + " send this : " + message); }) }); }); //The nice thing about Socket.io is that we don't have to worry about handling client disconnections. //When it disconnects, Socket.io will no longer receive responses to "heartbeat" messages //and will deactivate the session associated with the client. //If it was just a temporary disconnection, the client will reconnect and continue with the session.
If you wish to know more about node.js, please read the referred article by Guillaume Besson.
Nessun commento:
Posta un commento