mercoledì 16 aprile 2014

Node.js simple chat application > Node.js javascript

Following this nice tutorial (I'm a naive myself with node.js), I ended up having some problem due to the changes beetween express 2.x and express 3.x. I won't go into details, you can find anything you need to know in the previously linked article, here the modified and working code for current express, socket.io and jade implementation.

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.

 
Also in home.jade, the doctype 5 line is deprecated, use doctype html instead.
If you wish to know more about node.js, please read the referred article by .

giovedì 3 aprile 2014

Tips and Tricks for improving your work time

There are a lot of tricks and shortcuts that can help you be faster while programming. Most of the time we became tired of doing simple tasks by switching between keyboard and mouse. There are a lot of shortcuts that windows uses, most of them I've learned by friends when I was little, they where common during the time of windows 95/98, but now I don't see so many people using them.
I think it's important to relay on shortcuts and by time starting using them can really improve your work time!

Windows Keyboard Shortcuts 
Win+d Minimize all windows on all Monitors. Press again to restore previous state
Win+m Minimize all windows on current Monitor
Win+Shift+m Restore previously minimized windows on current Monitor
Win+Home Set all windows to Minimized on current Monitor except active
Win+Space Preview Desktop / make windows transparent (May not work with all Settings)
Alt+tab,
alt+Shift+Tab
Cycles through open programs in taskbar. Hold alt and continuously press tab to move forward between applications. Release keys to switch to application selected. Add shift to reverse direction.
Alt+Ctrl+tab, then use arrow keys to select application Cycles through open programs in taskbar without needing to hold alt continuously. Pressalt+ctrl+tab once, then continue with arrow keys and press enter on application.
Win+e Start Windows Explorer (in My Computer)
Win+r Open the Run window
Win+f Open Windows Search. f3 on empty desktop works, too.

A lot more windows keyboard shortcuts can be found here
Eclipse Keyboard Shortcuts 
Since I usually use eclipse as my default editor, I would like to list here more useful shortcuts that I usually like to use while programming.
 
Ctrl+Space Call the auto-complete tool, It's awesome. Just write the first letter of an object/method/anything and then press it.
Crtl+F11 Run the last launched class
Ctrl+1 Quick Fix
Ctrl+Shift+F Format the source code
Ctrl+Shift+O Imports missing classes
Shift+F2 Shows javadoc for selected class/ type or method
Ctrl+3 Quick access to any eclipse command
For a more complete list take a look here.

Eclipse Programming Tips 
 
Still I've a lot to learn about organizing my projects, but I assure you, following these simple tips can save you some time in the future.
  • Always make a local /lib directory in the project to store imports, this way you won't lose reference in the future.
  • Invest in creating static main() methods for the classes you want to test frequently. (Just configure different Runs in Run Configuration).
  • When searching for libraries online just takes too much, take a look to mavent 2 eclipse.
  • Always comment your code, because you will forget everything you have done sooner or later. In eclipse write /* on top of a class definition and then press ctrl+space to autocomplete the class description and specification. This text will appear in eclipse windows when importing/using the class.
  • Add stackoverflow to your browser preferences
***

For last, a lot of people seems to not know the utility of the End and Home key while writing, with these keys you can easily move at the end and begin of the current line.

That's what I daily use, if you can give me some other tips feel free to write a comment.