├── demo.html ├── json.js ├── README └── server.js /demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | socket.io demo 5 | 6 | 7 | 8 | 9 | 10 | 11 | 29 | 30 |

Sample pushJs

31 |

Prepare to be amazed

32 | 33 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /json.js: -------------------------------------------------------------------------------- 1 | if(!this.JSON){JSON=function(){function f(n){return n<10?'0'+n:n;} 2 | Date.prototype.toJSON=function(){return this.getUTCFullYear()+'-'+ 3 | f(this.getUTCMonth()+1)+'-'+ 4 | f(this.getUTCDate())+'T'+ 5 | f(this.getUTCHours())+':'+ 6 | f(this.getUTCMinutes())+':'+ 7 | f(this.getUTCSeconds())+'Z';};var m={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};function stringify(value,whitelist){var a,i,k,l,r=/["\\\x00-\x1f\x7f-\x9f]/g,v;switch(typeof value){case'string':return r.test(value)?'"'+value.replace(r,function(a){var c=m[a];if(c){return c;} 8 | c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+ 9 | (c%16).toString(16);})+'"':'"'+value+'"';case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';} 10 | if(typeof value.toJSON==='function'){return stringify(value.toJSON());} 11 | a=[];if(typeof value.length==='number'&&!(value.propertyIsEnumerable('length'))){l=value.length;for(i=0;iWelcome. Try the pushJs demo.'); 27 | res.end(); 28 | break; 29 | 30 | case '/json.js': 31 | case '/demo.html': 32 | fs.readFile(__dirname + path, function(err, data){ 33 | if (err) return send404(res); 34 | res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'}) 35 | res.write(data, 'utf8'); 36 | res.end(); 37 | }); 38 | break; 39 | 40 | case '/whoison': 41 | res.writeHead(200, {'Content-Type': 'text/html'}); 42 | res.write('

Welcome. These people are on

'); 43 | res.write(''); 49 | res.write( 50 | '
'+ 51 | 'Enter username: Must be from the list above.

'+ 52 | 'Enter javascript to send to the user: ex alert("hello");
'+ 53 | ''+ 54 | '
' 55 | ); 56 | 57 | res.end(); 58 | break; 59 | 60 | 61 | 62 | case '/myaction': 63 | 64 | if (req.method == 'POST') { 65 | console.log("[200] " + req.method + " to " + req.url); 66 | var fullBody = ''; 67 | 68 | req.on('data', function(chunk) { 69 | // append the current chunk of data to the fullBody variable 70 | fullBody += chunk.toString(); 71 | }); 72 | 73 | req.on('end', function() { 74 | 75 | // request ended -> do something with the data 76 | res.writeHead(200, "OK", {'Content-Type': 'text/html'}); 77 | 78 | // parse the received body data 79 | var decodedBody = querystring.parse(fullBody); 80 | 81 | // output the decoded data to the HTTP response 82 | res.write('Message sent
');
 83 | 		  for (var postkey in decodedBody){
 84 | 			  res.write(postkey + ":"+ decodedBody[postkey]+'
'); 85 | 86 | } 87 | 88 | redisMessage = {} 89 | redisMessage.username = decodedBody.username; 90 | redisMessage.rawjs = decodedBody.js; 91 | 92 | var mes = JSON.stringify(redisMessage); 93 | rclient.rpush(queueName, mes); 94 | 95 | res.write('
'); 96 | 97 | res.end(); 98 | }); 99 | 100 | } else { 101 | console.log("[405] " + req.method + " to " + req.url); 102 | res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); 103 | res.end('405 - Method not supported

Method not supported.

'); 104 | } 105 | break; 106 | 107 | 108 | default: send404(res); 109 | } 110 | }), 111 | 112 | send404 = function(res){ 113 | res.writeHead(404); 114 | res.write('404'); 115 | res.end(); 116 | }; 117 | 118 | server.listen(8081); 119 | 120 | 121 | var io = io.listen(server); 122 | 123 | 124 | var myUsers = {}; 125 | var myUsersSessionToId = {}; 126 | 127 | io.on('connection', function(client){ 128 | 129 | client.on('message', function(message){ 130 | 131 | try { 132 | if( "username" in message) { 133 | console.log("Error " + message.username); 134 | myUsers[message.username] = client; 135 | myUsersSessionToId[client.sessionId] = message.username; 136 | return; 137 | } 138 | } catch (e) { 139 | console.log(e); 140 | } 141 | }); 142 | 143 | client.on('disconnect', function(){ 144 | try { 145 | delete myUsers[myUsersSessionToId[client.sessionId]]; 146 | } catch (e) { 147 | console.log(e); 148 | } 149 | }); 150 | }); 151 | 152 | 153 | 154 | rBlockingClient.on("error", function (err) { 155 | console.log("Error " + err); 156 | }); 157 | 158 | rclient.on("error", function (err) { 159 | console.log("Error " + err); 160 | }); 161 | 162 | 163 | handleMessage = function(err,d) { 164 | try{ 165 | console.log("Got pushJS Message"+d); 166 | var jsonString = JSON.parse(d[1]) 167 | console.log(jsonString.username); 168 | myUsers[jsonString.username].send({ 'rawjs': jsonString.rawjs }); 169 | } catch (e) { 170 | console.log(e); 171 | } finally { 172 | rBlockingClient.blpop(queueName,0, handleMessage); 173 | } 174 | } 175 | 176 | rBlockingClient.blpop(queueName,0, handleMessage); 177 | 178 | --------------------------------------------------------------------------------