Permalink
Browse files

huge restructuration, still working (new client javascript, new metho…

…ds, updated readme, added config file)...
  • Loading branch information...
1 parent e27a682 commit 52433313a3b15b7e9692ed69714ab73c1beb2840 @daraosn committed May 6, 2012
Showing with 366 additions and 203 deletions.
  1. +0 −5 .project
  2. +15 −2 README.md
  3. +0 −116 app.js
  4. +2 −1 config.template.js
  5. +167 −0 mustekala.js
  6. +14 −8 package.json
  7. +47 −52 public/example.html
  8. +121 −9 public/mustekala.js
  9. +0 −10 public/mustekala/mustekala.js
View
@@ -5,11 +5,6 @@
<projects>
</projects>
<buildSpec>
- <buildCommand>
- <name>com.aptana.ide.core.unifiedBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
@@ -1,3 +1,5 @@
+*UPDATE: Working on new version, not ready for production yet*
+
Mustekala
======
@@ -12,9 +14,9 @@ Go to http://wehack.it and see it in action.
Example 2
-----
-* Clone node-puller on your computer
+* Clone mustekala on your computer
* Run npm install to get socket.io and Express
-* Run node app
+* Run node mustekala
* Go to http://localhost:3001/example.html and play with it using different browsers or clients.
* You can also use curl to make a POST request to push/pull events.
````curl -d "channel=yourchannelname&command=alert('hey')&password=yourpassword" http://localhost:3001/puller/run````
@@ -135,6 +137,17 @@ What is still missing:
* Testing
+Author
+------
+
+Diego Araos <d@wehack.it>
+
+Contributors
+------------
+
+Diego Araos
+Herman Junge
+
License
-----
View
@@ -1,116 +0,0 @@
-try {
- var config = require('./config.js');
-} catch(e) {
- console.log("No 'config.js' found. Use 'config.template.js' to create one.");
- process.exit(1);
-}
-
-var express = require('express');
-var app = module.exports = express.createServer();
-var io = require('socket.io').listen(app);
-
-configure();
-
-var channels=[];
-
-// Sockets
-var socket=io.of('/mustekala').on('connection', function(client) {
- //console.log('*** CLIENT CONNECTED ***')
- //client.emit('log','welcome')
- var currentChannel;
- var currentUserId;
- client.on('subscribe', function(channel,presenceToken,userData) {
- if(channel) {
- if(currentChannel) {
- client.leave(currentChannel);
- //client.emit('log','left channel: '+currentChannel);
- }
- client.join(channel);
- currentChannel=channel;
- client.emit('subscribed', channel);
- //client.emit('log','joined channel: '+channel)
-
- }
- });
- client.on('authentificate', function() {
-
- });
- client.on('run', function(password, channel, command) {
- socketRun(password, channel, command)
- });
- client.on('disconnect',function(){
- socket.emit('log','someone left!');
- });
-});
-
-function socketRun(password, channel, command) {
- if(password==config.password) {
- //client.emit('log','authorized')
- if(channel) {
- socket.to(channel).emit('run', command);
- //client.emit('log','broadcasted to channel ('+channel+'): '+command);
- } else {
- socket.emit('run', command);
- //client.emit('log','broadcasted to all: '+command);
- }
- return true;
- } else {
- //client.emit('log','not authorized')
- return false;
- }
-}
-
-function socketPresencePreauthorize(password) {
- if(password==config.password) {
- do { var token = global.token(); } while(presenceKeys[token]);
- presenceKeys[token] = new Date().getTime() + presenceKeyExpiry;
- // console.log(global.dump(presenceKeys));
- return token;
- } else {
- return false;
- }
-}
-
-app.post('/mustekala/run', function(req, res) {
- var password=req.body.password;
- var channel=req.body.channel;
- var command=req.body.command;
- var data=req.body.data;
- var result=socketRun(password, channel, command);
- res.send(JSON.stringify({'success':result, 'channel': channel, 'command': command, 'data': data}));
-});
-
-app.post('/mustekala/presence/preauthorize', function(req, res) {
- var password=req.body.password;
- var result=socketPreauthorize();
- res.send(JSON.stringify({'success':result}));
-});
-
-
-function configure() {
- // Configuration
- app.configure(function(){
- // app.set('views', __dirname + '/views');
- // app.set('view engine', 'jade');
- app.use(express.bodyParser());
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(express.static(__dirname + '/public'));
- });
-
- app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
- });
-
- app.configure('production', function(){
- // Disable /example.html on production
- app.get('/example.html', function(req, res) {
- res.redirect('/');
- });
- app.use(express.errorHandler());
- });
-}
-
-
-app.listen(3001);
-console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
@@ -1,3 +1,4 @@
module.exports={
- 'password': ''
+ 'password': 'bGINGS/(ADNfg78GASIMDbkASbj' // replace with your own
+ ,'port': 4567 // default: 4567
}
View
@@ -0,0 +1,167 @@
+var mustekala = {
+ 'version': '0.1.0'
+ ,'github': 'http://github.com/daraosn/mustekala'
+}
+
+try {
+ var config = require('./config.js');
+} catch(e) {
+ console.log("No 'config.js' found. Use 'config.template.js' to create one.");
+ process.exit(1);
+}
+
+var express = require('express');
+var app = module.exports = express.createServer();
+
+app.configure(function(){
+ // app.set('views', __dirname + '/views');
+ // app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ // app.get('/example.html', function(req, res) { res.redirect('/'); }); // Disable /example.html on production
+ app.use(express.errorHandler());
+});
+
+var io = require('socket.io').listen(app);
+io.configure('development', function(){
+ io.set('transports', ['websocket']);
+});
+io.configure('production', function(){
+ io.enable('browser client minification');
+ io.enable('browser client etag');
+ io.disable('browser client gzip'); // Mustekala won't support gzip yet
+ io.set('log level', 1);
+ io.set('transports', [
+ 'websocket'
+ , 'flashsocket'
+ , 'htmlfile'
+ , 'xhr-polling'
+ , 'jsonp-polling'
+ ]);
+});
+
+
+var channels=[];
+
+// Sockets
+var socket=io.of('/mustekala').on('connection', function(client) {
+ console.log('*** CLIENT CONNECTED ***');
+
+ client.emit('log','welcome')
+ var currentChannel;
+
+ client.on('subscribe', function(channel,presenceToken,userData) {
+ if(channel) {
+ if(currentChannel) {
+ client.leave(currentChannel);
+ client.emit('unsubscribe',currentChannel);
+ client.emit('log','left channel: '+currentChannel);
+ }
+ client.join(channel);
+ currentChannel=channel;
+ client.emit('subscribe', channel);
+ client.emit('log','joined channel: '+channel)
+
+ }
+ });
+ client.on('authentificate', function() {
+
+ });
+ client.on('run', function(password, channel, command, data) {
+ socketRun(password, channel, command, data)
+ });
+ client.on('disconnect',function(){
+ socket.emit('log','someone left!');
+ });
+});
+
+function socketRun(password, channel, command, data) {
+ if(password==config.password) {
+ // client.emit('log','authorized')
+ if(channel) {
+ socket.to(channel).emit('run', command);
+ //client.emit('log','broadcasted to channel ('+channel+'): '+command);
+ } else {
+ socket.emit('run', command);
+ //client.emit('log','broadcasted to all: '+command);
+ }
+ return true;
+ } else {
+ // client.emit('log','not authorized')
+ return false;
+ }
+}
+
+function socketPresencePreauthorize(password) {
+ if(password==config.password) {
+ do { var token = global.token(); } while(presenceKeys[token]);
+ presenceKeys[token] = new Date().getTime() + presenceKeyExpiry;
+ // console.log(global.dump(presenceKeys));
+ return token;
+ } else {
+ return false;
+ }
+}
+
+app.post('/mustekala/run', function(req, res) {
+ var password=req.body.password;
+ var channel=req.body.channel;
+ var command=req.body.command;
+ var data=req.body.data;
+ var result=socketRun(password, channel, command);
+ res.send(JSON.stringify({'success':result, 'channel': channel, 'command': command, 'data': data}));
+});
+
+app.post('/mustekala/presence/preauthorize', function(req, res) {
+ var password=req.body.password;
+ var result=socketPreauthorize();
+ res.send(JSON.stringify({'success':result}));
+});
+
+app.get('/mustekala.js', function(req, res) {
+ // we must "cheat" socket.io to get the socket.io.js with specific transports
+ var ioHead={};
+ var ioEnd={};
+ var fakeRes = {
+ writeHead: function(status, headers) {
+ ioHead.status=status;
+ ioHead.headers=headers;
+ },
+ end: function(content, encoding) {
+ ioEnd.content=content;
+ ioEnd.encoding=encoding;
+ finishRequest();
+ }
+ }
+ // fake uri
+ req.url="/socket.io/socket.io.js";
+ var data=io.checkRequest(req);
+ var mustekalaJS=require('fs').readFileSync(process.cwd()+'/public/mustekala.js');
+ io.static.write(data.path, req, fakeRes);
+ // send js
+ function finishRequest() {
+ // inject mustekala
+ ioEnd.content=mustekalaJS+"\n"+ioEnd.content;
+ ioHead.headers['Content-Length']=ioEnd.content.length;
+ res.writeHead(ioHead.status, ioHead.headers)
+ res.end(ioEnd.content, ioEnd.encoding);
+ }
+ // TODO: verify if socket.io.js is dynamic or not to cache js.
+});
+
+app.get('/', function(req, res) {
+ res.writeHead(200, {"Content-Type": "text/html"});
+ res.end('Mustekala v'+mustekala.version+'. Fork it at <a href="'+mustekala.github+'">'+mustekala.github+'</a>');
+});
+
+app.listen(config.port || 3000);
+console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
@@ -1,10 +1,16 @@
{
- "name": "application-name"
- , "version": "0.0.1"
- , "private": true
- , "dependencies": {
- "express": "2.5.4"
- , "jade": ">= 0.0.1"
- , "socket.io": "0.9.6"
+ "author": "Diego Araos <d@wehack.it> (http://wehack.it/)",
+ "name": "mustekala",
+ "description": "push/pull service for real-time apps",
+ "version": "0.0.1",
+ "homepage": "https://github.com/daraosn/mustekala",
+ "repository": {
+ "url": "git://github.com/daraosn/mustekala.git"
+ },
+ "dependencies": {},
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
}
-}
+}
Oops, something went wrong.

0 comments on commit 5243331

Please sign in to comment.