Skip to content

Commit

Permalink
tema2 actualizado y ejercicios
Browse files Browse the repository at this point in the history
  • Loading branch information
werelax committed Apr 22, 2014
1 parent b69298c commit d8ad98c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 4 deletions.
7 changes: 7 additions & 0 deletions ejercicios/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Reloj = require("./reloj").Reloj;
var reloj = new Reloj();

reloj.on("segundo", function(fecha) {
console.log("Un segundo! son las:", fecha);
reloj.removeAllListeners("segundo");
});
18 changes: 18 additions & 0 deletions ejercicios/cp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var fs = require("fs");

var origen = process.argv[2],
dest = process.argv[3];

var readStream = fs.createReadStream(origen, { flags: "r", autoClose: true});
var writeStream = fs.createWriteStream(dest, { flags: "w"});

// readStream.on("data", writeStream.write.bind(writeStream));
// readStream.on("end", writeStream.end.bind(writeStream));

readStream.on("data", function(chunk) {
writeStream.write(chunk);
});

readStream.on("end", function() {
writeStream.end();
});
15 changes: 15 additions & 0 deletions ejercicios/cuentalineas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var fs = require("fs");

var readStream = fs.createReadStream(process.argv[2],
{ flags: "r",
autoClose: true });

var contenido = "";

readStream.on("data", function(chunk) {
contenido += chunk;
});

readStream.on("end", function() {
console.log(contenido.split("\n").length - 1);
});
16 changes: 16 additions & 0 deletions ejercicios/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var EventEmitter = require("events").EventEmitter,
inherits = require("util").inherits;

function Tambor() {
var self = this;
setInterval(function() {
self.emit("pom!");
}, 1000);
};
inherits(Tambor, EventEmitter);

var tambor1 = new Tambor();

tambor1.on("pom!", function() {
console.log("El tambor suena!");
});
14 changes: 14 additions & 0 deletions ejercicios/hola.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var start = Date.now();

setTimeout(function() {
for (var i=Number.MAX_VALUE; i--;) {
Math.pow(12345, 123455);
}
}, 100);

setInterval(function() {
var now = Date.now();
console.log("Han pasado", now - start, "ms");
start = now;
console.log("Hola otra vez, Mundo del futuro!");
}, 1000);
9 changes: 9 additions & 0 deletions ejercicios/reloj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var EventEmitter = require("events").EventEmitter,
inherits = require("util").inherits;

function Reloj() {
this.emit("segundo", new Date());
}
inherits(Reloj, EventEmitter);

exports.Reloj = Reloj;
Binary file modified tema2.pdf
Binary file not shown.
15 changes: 14 additions & 1 deletion tema7/simple/public/code.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
$(function() {

/* Tu código aquí! */
var socket = io.connect("http://localhost:3000/"),
me = $.get("/me");

Chat.registerHandler(function(msg) {
var msgData = {text: msg, date: new Date()}
me.then(function(me) {
socket.emit("send:message", me, msgData);
Chat.showMyMsg(me, msgData);
})
})

socket.on("posted:message", function(user, msgData) {
Chat.postMsg(user, msgData);
})

});
19 changes: 16 additions & 3 deletions tema7/simple/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function extend() {
function gravatar(email, s) {
var baseUrl = "http://www.gravatar.com/avatar/",
parEmail = email.toLowerCase().trim(),
hash = crypto.createHash("md5").update(parEmail).digest("hex")
return baseUrl + hash + (s? "s="+s : "")
hash = crypto.createHash("md5").update(parEmail).digest("hex");
return baseUrl + hash + (s? "s="+s : "");
}

app.configure(function() {
Expand All @@ -33,13 +33,26 @@ app.configure(function() {

/* Routas */

app.post("/session", function(req, res) {
var userData = {}

app.post("/session", function(req, res) {
req.session.nick = req.body.nick;
req.session.avatar = gravatar(req.body.gravatar, 50);
res.redirect("/chat.html")
});

app.get("/me", function(req, res) {
res.send({name: req.session.nick, avatar: req.session.avatar})
})

app.use(function(req, res) { res.redirect("/login.html"); });

/* WebSockets */

io.sockets.on("connection", function(socket) {
socket.on("send:message", function(user, msgData) {
socket.broadcast.emit("posted:message", user, msgData)
})
})

server.listen(3000);

0 comments on commit d8ad98c

Please sign in to comment.