Skip to content

Instantly share code, notes, and snippets.

@alanpich
Created January 24, 2014 17:29
Show Gist options
  • Save alanpich/8602036 to your computer and use it in GitHub Desktop.
Save alanpich/8602036 to your computer and use it in GitHub Desktop.
Node/Phantom one-way socket bridge
var PhantomBridgeClient,
net = require('net');
module.exports = PhantomBridgeClient = function(socketPath){
var socket;
function write(str){
socket.write(str);
}
/**
* Start connection
* @param cb
*/
this.open = function(cb){
socket = net.createConnection({
path: socketPath
},cb);
};
this.close = function(cb){
socket.end();
}
this.send = function(type,data){
var json = JSON.stringify({
type: type,
data: data
});
write(json);
};
this.sendRaw = function(str){
write(str);
};
};
var config = require('./config'),
socket = require('./lib/phantom-bridge').client(config.sockPath),
json = process.argv[2];
socket.open(function(){
// Send a nicely prepared message ready to be read
socket.send('console',{
level: 'warn',
ns: 'WARN',
msg: 'This is the message to log to the console'
})
// Or send a raw string - server will throw error if
// string isn't JSON though!
socket.sendRaw(json);
// Close the socket connection or script will run forever
socket.close();
});
var config = require('./config'),
socket = require('./lib/phantom-bridge').server(config.sockPath);
/**
* Report when the socket is ready and listening
*/
socket.on('listening',function(){
console.log('Listening on socket');
});
/**
* Listen for errors
*
* @param err {Error}
*/
socket.on('error',function(err){
console.log('[ERROR] ',err.stack);
});
/**
* Register a handler for 'console' messages
*
* @param msg {Object} Message properties
*/
socket.registerMessageHandler('console',function(msg){
console.log('['+msg.ns.toUpperCase()+'] '+msg.msg);
});
/**
* Start the server
*/
socket.start();
var Server = require('./server'),
Client = require('./client');
module.exports = {
server: function(socketPath){
return new Server(socketPath);
},
client: function(socketPath){
return new Client(socketPath);
}
}
var net = require('net'),
fs = require('fs'),
events = require('events'),
_ = require('lodash'),
util = require('util');
/**
* Deletes a socket file at path
*
* @param path {String} File path to socket
*/
function deleteSocket(path){
if(fs.existsSync(path)){
fs.unlinkSync(path);
}
}
var PhantomBridge = function(path){
var server,
that = this,
handlers = {};
deleteSocket(path);
server = net.createServer(onNewConnection);
function onNewConnection (c){
that.emit('connect',c);
c.on('data',onDataReceived);
}
function onDataReceived(msg){
// Parse msg for JSON
try {
var data = JSON.parse(msg);
// that.emit('message',data);
// Try to find a registered handler
var type = data.type.toLowerCase(),
msgData = data.data;
if(handlers[type]){
handlers[type](data.data);
}
} catch (err){
that.emit('error',err);
}
}
this.start = function(){
server.listen(path,function(){
that.emit('listening');
});
};
this.registerMessageHandler = function(type,fn){
handlers[type] = fn;
}
};
util.inherits(PhantomBridge,events.EventEmitter);
module.exports = PhantomBridge;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment