gun/lib/ws.js
2016-01-28 13:00:38 -08:00

41 lines
1.3 KiB
JavaScript

var Gun = require('../gun')
, url = require('url');
module.exports = function(wss, server){
wss.on('connection', function(ws){
var req = {};
ws.upgradeReq = ws.upgradeReq || {};
req.url = url.parse(ws.upgradeReq.url||'');
req.method = (ws.upgradeReq.method||'').toLowerCase();
req.headers = ws.upgradeReq.headers || {};
//Gun.log("wsReq", req);
ws.on('message', function(msg){
msg = Gun.obj.ify(msg);
msg.url = msg.url || {};
msg.url.pathname = (req.url.pathname||'') + (msg.url.pathname||'');
Gun.obj.map(req.url, function(val, i){
msg.url[i] = msg.url[i] || val; // reattach url
});
msg.method = msg.method || req.method;
msg.headers = msg.headers || {};
Gun.obj.map(req.headers, function(val, i){
msg.headers[i] = msg.headers[i] || val; // reattach headers
});
server.call(ws, msg, function(reply){
if(!ws || !ws.send || !ws._socket || !ws._socket.writable){ return }
reply = reply || {};
if(msg && msg.headers && msg.headers['ws-rid']){
(reply.headers = reply.headers || {})['ws-rid'] = msg.headers['ws-rid'];
}
try{ws.send(Gun.text.ify(reply));
}catch(e){} // juuuust in case.
});
});
ws.off = function(m){
//Gun.log("ws.off", m);
ws.send = null;
}
ws.on('close', ws.off);
ws.on('error', ws.off);
});
}