Skip to content

Commit

Permalink
huge restructuration, still working (new client javascript, new metho…
Browse files Browse the repository at this point in the history
…ds, updated readme, added config file)...
  • Loading branch information
daraosn committed May 6, 2012
1 parent e27a682 commit 5243331
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 203 deletions.
5 changes: 0 additions & 5 deletions .project
Expand Up @@ -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>
Expand Down
17 changes: 15 additions & 2 deletions README.md
@@ -1,3 +1,5 @@
*UPDATE: Working on new version, not ready for production yet*

Mustekala
======

Expand All @@ -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````
Expand Down Expand Up @@ -135,6 +137,17 @@ What is still missing:

* Testing

Author
------

Diego Araos <d@wehack.it>

Contributors
------------

Diego Araos
Herman Junge

License
-----

Expand Down
116 changes: 0 additions & 116 deletions app.js

This file was deleted.

3 changes: 2 additions & 1 deletion config.template.js
@@ -1,3 +1,4 @@
module.exports={
'password': ''
'password': 'bGINGS/(ADNfg78GASIMDbkASbj' // replace with your own
,'port': 4567 // default: 4567
}
167 changes: 167 additions & 0 deletions mustekala.js
@@ -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);
22 changes: 14 additions & 8 deletions package.json
@@ -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": "*"
}
}
}

0 comments on commit 5243331

Please sign in to comment.