Bits of Discovery

Stuff I Like to Learn About

2048 Mod

So the 2048 craze has pretty well taken hold, so I decided to make a little mod myself out of it. The idea is that every 10 seconds, everyone playing accept everyone else’s keystrokes for a brief moment. I did this using Socket.io along with Node.js to send movements across clients. The main bit of code on the server involves adding and removing the event listener the appropriate intervals:

1
2
3
4
5
6
7
8
9
10
var cb = function (direction) {
  socket.broadcast.emit('keypress', direction);
};

setInterval(function () {
  socket.addListener('keypress', cb);
  setTimeout(function () {
    socket.removeListener('keypress', cb);
  }, 100);
}, 10000);

I also added a display so you can know how many other players are online! You can check it out here.