Created
August 21, 2012 16:04
Revisions
-
willyaranda revised this gist
Sep 20, 2012 . 1 changed file with 80 additions and 47 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,65 +6,98 @@ console.log('post-to-websocket --> Listening on 0.0.0.0:8888'); function onHTTPMessage(req, res) { console.log('post-to-websocket --> Received request for ' + req.url); if(req.url == "/websocket") { console.log("Petition accepted in /websocket"); if (req.method == 'POST') { var fullBody = ''; req.on('data', function(chunk) { fullBody += chunk.toString(); }); req.on('end', function() { var uatoken = null; try { uatoken = JSON.parse(fullBody).uatoken || JSON.parse(fullBody).data.uatoken; } catch(e) { console.log('no se puede parsear la petición'); return res.end('no se puede parsear la petición'); } websocket(uatoken, fullBody, function(salida) { console.log("salida para post --> " + salida); res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); return res.end(salida); }); return; }); } else { res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Petition in /websocket not a POST"); return res.end(); } } else { res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Malformed petition"); return res.end(); } } var connectionTable = {}; var connectionsText = {}; function websocket(uatoken, text, callback) { if (!connectionsText[uatoken]) connectionsText[uatoken] = ''; var messageType = ''; try { messageType = JSON.parse(text).messageType; } catch(e) { console.log('no se puede parsear respuesta de websocket'); } if (messageType == 'getAllMessages') { console.log('Getting getAllMessages for the connection of the ua ' + uatoken); var ret = connectionsText[uatoken]; connectionsText[uatoken] = ''; return callback(ret); } console.log('Buscando conexión para uatoken=' + uatoken); var connection = connectionTable[uatoken]; if (!connection) { console.log('Creating a new client…'); var WebSocketClient = require('websocket').client; connectionTable[uatoken] = new WebSocketClient(); client = connectionTable[uatoken]; client.on('connectFailed', function(error) { console.log('Connect Error: ' + error.toString()); }); client.on('connect', function(connection) { connectionTable[uatoken] = connection; console.log('WebSocket client connected'); connectionTable[uatoken].on('error', function(error) { console.log("Connection Error: " + error.toString()); }); connectionTable[uatoken].on('close', function() { console.log('push-notification Connection Closed'); connectionTable[uatoken] = null; }); connectionTable[uatoken].on('message', function(message) { if (message.type === 'utf8') { console.log("Received: '" + message.utf8Data + "'"); //connectionTable[uatoken].close(); connectionsText[uatoken] += message.utf8Data; callback(message.utf8Data); } }); if (connectionTable[uatoken].connected) { connectionTable[uatoken].sendUTF(text); } }); client.connect('ws://localhost:8080/', 'push-notification'); } else { console.log('Ya teníamos cliente, lo enviamos por ahí'); connection.sendUTF(text); } } function parseURL(url) { -
willyaranda revised this gist
Aug 21, 2012 . 1 changed file with 46 additions and 46 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,69 +5,69 @@ http.createServer(onHTTPMessage).listen(8888, "0.0.0.0"); console.log('post-to-websocket --> Listening on 0.0.0.0:8888'); function onHTTPMessage(req, res) { console.log('post-to-websocket --> Received request for ' + req.url); switch(req.url) { case "/websocket": console.log("Petition accepted in /websocket"); if (req.method == 'POST') { var fullBody = ''; req.on('data', function(chunk) { fullBody += chunk.toString(); }); req.on('end', function() { websocket(fullBody, function(salida) { console.log("salida para post --> " + salida); res.setHeader("access-control-allow-origin", "*"); res.end(salida); }); }); } else { res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Petition in /websocket not a POST"); res.end(); } break; default: res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Malformed petition"); res.end(); } } function websocket(text, callback) { console.log("Initiating websocket"); var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('connectFailed', function(error) { console.log('Connect Error: ' + error.toString()); }); client.on('connect', function(connection) { console.log('WebSocket client connected'); connection.on('error', function(error) { console.log("Connection Error: " + error.toString()); }); connection.on('close', function() { console.log('echo-protocol Connection Closed'); }); connection.on('message', function(message) { if (message.type === 'utf8') { console.log("Received: '" + message.utf8Data + "'"); callback(message.utf8Data); } }); if (connection.connected) { connection.sendUTF(text); } }); client.connect('ws://localhost:8080/', 'push-notification'); } function parseURL(url) { var urlparser = require('url'); return urlparser.parse(url); } -
willyaranda revised this gist
Aug 21, 2012 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,3 @@ var WebSocketServer = require('websocket').server; var http = require('http'); @@ -61,6 +58,10 @@ client.on('connect', function(connection) { callback(message.utf8Data); } }); if (connection.connected) { connection.sendUTF(text); } }); client.connect('ws://localhost:8080/', 'push-notification'); -
willyaranda revised this gist
Aug 21, 2012 . 1 changed file with 0 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,15 +61,6 @@ client.on('connect', function(connection) { callback(message.utf8Data); } }); }); client.connect('ws://localhost:8080/', 'push-notification'); -
willyaranda created this gist
Aug 21, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ //Requires to install the websocket module with npm: // $ npm install websocket // It runs even without the native extensions. Just pure node.js awesomeness var WebSocketServer = require('websocket').server; var http = require('http'); http.createServer(onHTTPMessage).listen(8888, "0.0.0.0"); console.log('post-to-websocket --> Listening on 0.0.0.0:8888'); function onHTTPMessage(req, res) { console.log('post-to-websocket --> Received request for ' + req.url); switch(req.url) { case "/websocket": console.log("Petition accepted in /websocket"); if (req.method == 'POST') { var fullBody = ''; req.on('data', function(chunk) { fullBody += chunk.toString(); }); req.on('end', function() { websocket(fullBody, function(salida) { console.log("salida para post --> " + salida); res.setHeader("access-control-allow-origin", "*"); res.end(salida); }); }); } else { res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Petition in /websocket not a POST"); res.end(); } break; default: res.writeHead(200, {'Content-Type': 'text/plain', 'access-control-allow-origin': '*'}); res.write("Malformed petition"); res.end(); } } function websocket(text, callback) { console.log("Initiating websocket"); var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('connectFailed', function(error) { console.log('Connect Error: ' + error.toString()); }); client.on('connect', function(connection) { console.log('WebSocket client connected'); connection.on('error', function(error) { console.log("Connection Error: " + error.toString()); }); connection.on('close', function() { console.log('echo-protocol Connection Closed'); }); connection.on('message', function(message) { if (message.type === 'utf8') { console.log("Received: '" + message.utf8Data + "'"); callback(message.utf8Data); } }); function sendNumber() { if (connection.connected) { var number = Math.round(Math.random() * 0xFFFFFF); connection.sendUTF(number.toString()); setTimeout(sendNumber, 1000); } } sendNumber(); }); client.connect('ws://localhost:8080/', 'push-notification'); } function parseURL(url) { var urlparser = require('url'); return urlparser.parse(url); }