Permalink
Browse files

Add README.

  • Loading branch information...
1 parent b5531f0 commit 3adc6bbf3a3650f3992dabd3bcadb6ac0c1b5777 @mranney committed Dec 17, 2010
Showing with 8 additions and 149 deletions.
  1. +2 −2 10.js
  2. +0 −143 16.js
  3. +1 −0 7.js
  4. +2 −2 8.js
  5. +2 −2 9.js
  6. +1 −0 README.md
View
4 10.js
@@ -1,4 +1,4 @@
-// Using Redis for stats and logging
+// Using Redis for stats and logging - demo.ranney.com
var http = require("http"),
fs = require("fs"),
@@ -72,4 +72,4 @@ server = http.createServer(function (request, response) {
}
});
-server.listen(9000);
+server.listen(9000); // demo.ranney.com
View
143 16.js
@@ -1,143 +0,0 @@
-// Chat Server With Redis Sauce
-
-var http = require("http"),
- fs = require("fs"),
- util = require("util"),
- redis = require("redis"), client,
- ws = require("websocket-server"),
- http_server, ws_server, favicon, client_js, client_html, start = Date.now(),
- clients = {};
-
-favicon = fs.readFileSync("favicon.ico");
-client_js = fs.readFileSync("chat_client.js");
-client_html = fs.readFileSync("chat_client.html");
-
-redis_client = redis.createClient();
-
-function log_request(req) {
- var ua = req.headers["user-agent"] || "";
-
- redis_client.hincrby("url", req.url, 1);
- redis_client.hincrby("ip", req.connection.remoteAddress, 1);
- if (ua) {
- redis_client.hincrby("ua", ua, 1);
- }
- redis_client.publish("log", (Date.now() - start) + ", " +
- req.connection.remoteAddress + " " +
- req.url + " " + ua);
-}
-
-function serve_buffer(res, buf, type) {
- res.writeHead(200, {
- "Content-Type": type
- });
- res.end(buf);
-}
-
-function stats(res) {
- var ip, url, ua;
-
- res.writeHead(200, {
- "Content-Type": "text/plain"
- });
-
- redis_client.hgetall("ip", function (err, reply) {
- ip = reply;
- });
- redis_client.hgetall("ua", function (err, reply) {
- ua = reply;
- });
- redis_client.hgetall("url", function (err, reply) {
- url = reply;
- res.end(JSON.stringify({
- ip: ip,
- url: url,
- ua: ua
- }));
- })
-}
-
-function image_file(res) {
- res.writeHead(200, {
- "Content-Type": "image/jpeg"
- });
-
- fs.createReadStream("anchor.jpg").pipe(res);
-}
-
-http_server = http.createServer(function (request, response) {
- log_request(request);
-
- if (request.url === "/favicon.ico") {
- serve_buffer(response, favicon, "image/x-icon");
- } else if (request.url === "/") {
- serve_buffer(response, client_html, "text/html");
- } else if (request.url === "/client.js") {
- serve_buffer(response, client_js, "application/javascript");
- } else if (request.url === "/stats") {
- stats(response);
- } else {
- image_file(response);
- }
-});
-
-function new_ws_client(conn) {
- var addr = conn._socket.remoteAddress + ":" + conn._socket.remotePort;
-
- redis_client.zadd("presence", Date.now(), addr);
- redis_client.publish("presence update", addr + " connected");
-
- conn.on("close", function () {
- closed_ws_client(addr);
- });
-
- conn.on("message", function (msg) {
- var obj = JSON.parse(msg);
- if (obj.op === "partial") {
- ws_server.broadcast(JSON.stringify({
- op: obj.op,
- text: obj.text,
- id: addr
- }));
- } else if (obj.op === "text message") {
- redis_client.lpush("messages_" + addr, obj.text);
- ws_server.broadcast(JSON.stringify({
- op: obj.op,
- text: obj.text,
- id: addr
- }));
- }
- });
-
- clients[addr] = true;
- ws_server.broadcast(JSON.stringify({
- op: "connected",
- id: addr
- }));
-
- Object.keys(clients).forEach(function (item) {
- conn.write(JSON.stringify({
- op: "connected",
- id: item
- }));
- });
-}
-
-function closed_ws_client(addr) {
- redis_client.zrem("presence", addr);
- redis_client.publish("presence update", addr + " disconnected");
-
- ws_server.broadcast(JSON.stringify({
- op: "disconnected",
- id: addr
- }));
-
- delete clients[addr];
-}
-
-ws_server = ws.createServer({
- server: http_server
-});
-
-ws_server.on("connection", new_ws_client);
-ws_server.listen(4000);
View
1 7.js
@@ -11,4 +11,5 @@ client.get("some key", function (err, reply) {
client.hmset("hash key", "prop1", "val1", "prop2", "val2");
client.hgetall("hash key", function (err, res) {
console.log("Val: " + JSON.stringify(res));
+ client.quit();
});
View
4 8.js
@@ -1,4 +1,4 @@
-// Using Redis for stats
+// Using Redis for stats - demo.ranney.com
var http = require("http"),
fs = require("fs"),
@@ -43,4 +43,4 @@ server = http.createServer(function (request, response) {
}
});
-server.listen(9000);
+server.listen(9000); // demo.ranney.com
View
4 9.js
@@ -1,4 +1,4 @@
-// Using Redis for stats and logging
+// Using Redis for stats and logging - demo.ranney.com
var http = require("http"),
fs = require("fs"),
@@ -47,4 +47,4 @@ server = http.createServer(function (request, response) {
}
});
-server.listen(9000);
+server.listen(9000); // demo.ranney.com
View
@@ -0,0 +1 @@
+These examples were written and tested against node 0.3.x. They don't quite work against node 0.2.x, but they should still be useful examples of how to use Redis with node.

0 comments on commit 3adc6bb

Please sign in to comment.