diff options
-rw-r--r-- | notes.txt | 13 | ||||
-rw-r--r-- | protoype/app-sio.js | 121 | ||||
-rw-r--r-- | socket.io/public/index.html | 17 | ||||
-rw-r--r-- | socket.io/sio1.js | 19 | ||||
-rw-r--r-- | socket.io/sio2.js | 24 | ||||
-rw-r--r-- | socket.io/sio3.js | 55 |
6 files changed, 249 insertions, 0 deletions
@@ -9,6 +9,14 @@ $ express id is 'listdb', then a db would get preloaded by the time we stepped into the route logic - doing next(new Error('foo')) will stall and end middleware chain +[node] +- process.nextTick() + when you defined a function that return something, and takes in a callback, + make sure you callback on the next event loop-around, so at that point you + would return valid object, that callback might likely want to access/modify + + use this to be nice, similar to yield()? delay executation of callback until next loop-around + [code analysis] # npm install -g jslint $ jslint --sloppy --white <file.js> @@ -24,6 +32,9 @@ var bar = foo.slice(0); who responds/listens to those, so we will see statements such eventer.on('foo', somehandler) +[modules] +- now.js and dnode--socket.io namespace (function/var sharing) + [mongodb] $ mongo; show dbs; use nodejs1; show collections; db.getCollectionNames(), db.users.find(), db.users.remove(), db.users.drop() @@ -32,6 +43,8 @@ $ node --debug-brk app.js $ node-inspector & http://0.0.0.0:8080/debug?port=5858 +- __proto__ in node-inspector imples functions/methods of the 'this' object + [module.exports vs exports] - if you want your module to be of a specific object type (boolean, string, etc), use module.exports; if you want your module to be a typical module instance, use exports diff --git a/protoype/app-sio.js b/protoype/app-sio.js new file mode 100644 index 0000000..61c9958 --- /dev/null +++ b/protoype/app-sio.js @@ -0,0 +1,121 @@ +/* + * kyle's nodejs server + * + * by default app.settings.env is set to 'development' (process.env.ENV_VARIABLE) + * $ NODE_ENV=production node app.js + * + * ToDo: + * - redis for active users + * - load from db once, and refetch when necessary + * - if (verbose) log; can choose to use process.env and/or app.settings.env + * - jsdoc? + * + */ + +var util = require('util'); +var crypto = require('crypto') +var path = require('path'); +var http = require('http'); + +var express = require('express'); +var io = require('socket.io'); +var RedisStore = require('connect-redis')(express); + +var db = require('./mydb.js'); +var myplatform = require('./router/myplatform.js'); +var user = require('./router/user.js'); +var index = require('./router/index.js'); + +var app = express(); /* return function accepting req/res */ +var server = http.createServer(app); /* this method takes in a function/callback that it feeds with req/res on 'request' event */ +var sio = io.listen(server); + +function deadend(req, res, next) { + util.log('[deadend] couldn\'t serve, requested path: ' + req.url); + /* collect possible info here */ + /* if (critical_wrong) then; throw new Error('da fuck this entity is doing!'); */ + res.send(404, 'page not found\n'); +} + +function error_handler(err, req, res, next) { /* error handling, arity of 4 */ + console.error(err.stack); + res.send(500, 'something broke!\n'); +} + +/* delete req.session.user on close connection? */ +function restrict(req, res, next) { + if (req.session.user) + { + util.log('[restrict] granted ' + req.session.user); + next(); + } + else + { + util.log('[restrict] blocked access'); + res.send(401, 'access restricted\n'); + /* res.redirect(/login); */ + } +} + +app.configure(function() { + app.set('views', __dirname + '/views'); + app.set('view engine', 'jade'); /* no need to specify .jade extension */ + app.set('view options', { pretty: true }); /* avoid html source all in on line or so */ + + app.use(express.logger('dev')); + app.use(express.favicon()); + app.use(express.compress()); /* gzip */ + app.use(express.bodyParser()); /* creates req.body which req.param() uses */ + app.use(express.cookieParser()); /* req.session.* can be populated with user defined vars */ + app.use(express.session({ secret: "keyboard cat", store: new RedisStore() })); /* populates req.session */ + app.use(app.router); /* when there's no match, we go static file handling below */ + app.use(require('stylus').middleware(__dirname + '/public')); + app.use(express.static(path.join(__dirname, 'public'))); /* GET /stylesheets/style.css */ + app.use(deadend); /* we get here if we couldn't serve */ + app.use(error_handler); /* is this correct? */ +}); + +app.get('/', index.root); +app.get('/create', user.create_get); +app.post('/create', user.create_post); +app.get('/login', user.login_get); +app.post('/login', user.login_post); +//app.all('*', auth.check); /* not applicable, I want router list to hit the end in case auth fails */ +app.get('/sys/:id([a-z]+)', restrict, myplatform.system); + +server.listen(8081, function() { + util.log(util.format('[server] listening on port %d in %s mode', this.address().port, app.settings.env)); +}) +.on('error', function(e) { + util.log('[server] failed creating server, errno: ' + e.errno); +}) +.on('close', function() { + util.log('[server] server shutdown'); +}) +.on('clientError', function(exception) { + util.log('[server] ' + exception); +}) +.on('connection', function(socket) { + /* socket.myid = crypto.createHash('md5').update(socket.remoteAddress + ':' + socket.remotePort).digest('hex'); */ + socket.myip = socket.remoteAddress; + socket.myport = socket.remotePort; + util.log('[server] new node connection from ' + socket.remoteAddress + ':' + socket.remotePort); + socket.on('close', function() { + /* delete connected_clients */ + util.log('[server] client ' + this.myip + ':' + this.myport + ' closed node connection\n'); + }); +}); + +sio.sockets.on('connection', function(socket) { + var endpoint = socket.manager.handshaken[socket.id].address; + /* socket.myid = crypto.createHash('md5').update(socket.remoteAddress + ':' + socket.remotePort).digest('hex'); */ + /* user defined fields */ + socket.myip = endpoint.address; + socket.myport = endpoint.port; + util.log('[server] new socket.io connection from ' + endpoint.address + ':' + endpoint.port); + socket.on('disconnect', function() { + /* delete connected_clients */ + util.log('[server] client ' + this.myip + ':' + this.myport + ' disconnected from socket.io connection\n'); + }); +}); + diff --git a/socket.io/public/index.html b/socket.io/public/index.html new file mode 100644 index 0000000..e8b4cf7 --- /dev/null +++ b/socket.io/public/index.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> +<head> + <title>socket.io</title> + <script src="/socket.io/socket.io.js"></script> + <script> + var socket = io.connect('http://localhost:8081'); + socket.on('news', function (msg) { + console.log(msg); + socket.emit('my other event', msg + ' back'); + }); + </script> +</head> + <body> + <p>hello socket.io.</p> + </body> +</html> diff --git a/socket.io/sio1.js b/socket.io/sio1.js new file mode 100644 index 0000000..1fa53d2 --- /dev/null +++ b/socket.io/sio1.js @@ -0,0 +1,19 @@ +var express = require('express'); +var http = require('http'); +var io = require('socket.io'); + +var app = express(); +var server = app.listen(8080); /* expresss .listen returns a http server? */ +var sio = io.listen(server); + +app.get('/', function (req, res) { + res.send('hello, world!'); +}); + +sio.sockets.on('connection', function (socket) { + socket.emit('news', { hello: 'world' }); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + diff --git a/socket.io/sio2.js b/socket.io/sio2.js new file mode 100644 index 0000000..6d44c37 --- /dev/null +++ b/socket.io/sio2.js @@ -0,0 +1,24 @@ +/* probably want to use this approach for socket.io */ + +var express = require('express'); +var http = require('http'); +var io = require('socket.io'); +var path = require('path'); + +var app = express(); +var server = http.createServer(app).listen(8081); +var sio = io.listen(server); + +app.use(express.static(path.join(__dirname, 'public'))); + +app.get('/', function (req, res) { + res.send('hello, world!'); +}); + +sio.sockets.on('connection', function (socket) { + socket.emit('news', 'hello there stranger'); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + diff --git a/socket.io/sio3.js b/socket.io/sio3.js new file mode 100644 index 0000000..56e3cbe --- /dev/null +++ b/socket.io/sio3.js @@ -0,0 +1,55 @@ +/* probably want to use this approach for socket.io */ + +var express = require('express'); +var http = require('http'); +var io = require('socket.io'); +var path = require('path'); +var util = require('util'); + +var app = express(); +var server = http.createServer(app).listen(8081); +var sio = io.listen(server); + +app.use(express.logger('dev')); +app.use(express.favicon()); + +app.all('*', function(req, res, next) { + util.log('trace, url: ' + req.url); + console.log('you are %s:%s and your socket is %s', res.connection.myip, res.connection.myport, res.connection.mytag); + next(); +}); + +app.get('/hello', function (req, res) { + /* console.log(util.inspect(req, false, 7)); */ + res.send('hello, world!'); +}); + +app.use(express.static(path.join(__dirname, 'public'))); + +/* teh fuck why this also gets invoked, fucking nodejs and js, clusterfuck god damn it */ +server.on('connection', function(socket) { + /* socket.myid = crypto.createHash('md5').update(socket.remoteAddress + ':' + socket.remotePort).digest('hex'); */ + util.log('[server] new node connection from ' + socket.remoteAddress + ':' + socket.remotePort); + socket.myip = socket.remoteAddress; + socket.myport = socket.remotePort; + socket.mytag = 'node socket'; + socket.on('close', function() { + /* delete connected_clients */ + util.log('[server] client ' + this.myip + ':' + this.myport + ' closed node connection\n'); + /* this.port2? why this keyword? above callback gets executed in socket object, that's why this works? */ + }); +}) + +sio.sockets.on('connection', function (socket) { + var endpoint = socket.manager.handshaken[socket.id].address; + util.log('[server] new socket.io connection from ' + endpoint.address + ':' + endpoint.port); + socket.myip = endpoint.address; + socket.myport = endpoint.port; + socket.mytag = 'socket.io socket'; + /* console.log(util.inspect(socket, false, 7)); find ip here */ + socket.emit('news', 'hello there stranger'); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + |