Skip to content

Commit

Permalink
ported old code to new templates and such
Browse files Browse the repository at this point in the history
  • Loading branch information
robrighter committed Aug 10, 2012
1 parent d2133eb commit c148e74
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 14 deletions.
156 changes: 151 additions & 5 deletions server.js
Expand Up @@ -4,6 +4,9 @@ var connect = require('connect')
, io = require('socket.io') , io = require('socket.io')
, port = (process.env.PORT || 8081); , port = (process.env.PORT || 8081);


//channels
var datastore = {};

//Setup Express //Setup Express
var server = express.createServer(); var server = express.createServer();
server.configure(function(){ server.configure(function(){
Expand Down Expand Up @@ -42,32 +45,175 @@ var io = io.listen(server);
io.sockets.on('connection', function(socket){ io.sockets.on('connection', function(socket){
console.log('Client Connected'); console.log('Client Connected');
socket.on('message', function(data){ socket.on('message', function(data){
socket.broadcast.emit('server_message',data); switch(data.cmd){
socket.emit('server_message',data); case 'connect':
console.log('GOT A CONNECT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
//do something
connectChannel(data.channel, socket);
break;
case 'saveresponse':
updateResponseText(data.channel, data.responsetext)
break;
default:
//do something
}
}); });
socket.on('disconnect', function(){ socket.on('disconnect', function(){
console.log('Client Disconnected.'); console.log('Client Disconnected.');
}); });
}); });


function connectChannel(thechannelname, client){
if(datastore.hasOwnProperty(thechannelname)){
//the channel already exists
console.log('Connected to Existing Channel: ' + thechannelname);
console.log('ResponseText for this channel is: ' + datastore[thechannelname].responseText);
datastore[thechannelname].clients.push(client);
}
else{
//the channel does not exist
console.log('Creating New Channel: ' + thechannelname);
datastore[thechannelname] = {
clients : [client],
responseText : ''
};
}
//set the callback to remove the client whenever it disconnects
client.on('disconnect', function(){
console.log('Client Disconnected from channel: ' + thechannelname);
var toremove = datastore[thechannelname].clients.indexOf(client);
console.log('going to delete item at index ' + toremove);
datastore[thechannelname].clients.splice(toremove,1);
});
return datastore[thechannelname];
}

function updateResponseText(thechannelname, responsetext){
if(datastore.hasOwnProperty(thechannelname)){
datastore[thechannelname].responseText = responsetext;
}
}



/////////////////////////////////////////// ///////////////////////////////////////////
// Routes // // Routes //
/////////////////////////////////////////// ///////////////////////////////////////////


/////// ADD ALL YOUR ROUTES HERE ///////// /////// ADD ALL YOUR ROUTES HERE /////////


//homepage
server.get('/', function(req,res){ server.get('/', function(req,res){
res.render('index.jade', { res.render('index.jade', {
locals : { locals : {
title : 'Your Page Title' header: ''
,description: 'Your Page Description' ,footer: ''
,author: 'Your Name' ,title : 'HTTP Request Inspector'
,description: 'A website for inspecting HTTP GETS and POSTS'
,author: 'Rob Righter'
,analyticssiteid: 'XXXXXXX' ,analyticssiteid: 'XXXXXXX'
} }
}); });
}); });


server.get("/:channel/console", function (req, res) {
//render out the console template and nothing else. The Channel is created and handled on the web socket event
res.render('console.jade', {
locals : {
header: '',
footer: '',
title : (req.params.channel +' Console'),
description: '',
author: 'Rob Righter',
channelname: req.params.channel,
responsetext: getResponseText(req.params.channel),
analyticssiteid: 'XXXXXXX'
}
});

});

server.get("/:channel", function (req, res, match) {
//render reply with whatever they asked us to.
sendConsoleUpdateToClients(req.params.channel, formatConsoleEntry(req));
sendResponseText(req.params.channel,res);
});

server.post("/:channel", function (req, res, match) {
//render reply with whatever they asked us to.
sendConsoleUpdateToClients(req.params.channel, formatConsoleEntry(req));
sendResponseText(req.params.channel,res);
});


function getResponseText(thechannelname){
return (datastore.hasOwnProperty(thechannelname) ? datastore[thechannelname].responseText : '');
}

function sendResponseText(thechannelname,res){
if(datastore.hasOwnProperty(thechannelname)){
//the channel exists
res.send(datastore[thechannelname].responseText);
}
else{
//the channel does not exist so just send out an error
res.send('{"error" : "Sorry, this channel does not exist"}');
}
}

function sendConsoleUpdateToClients(channel, consolestring){
var message = {
cmd: 'consoleupdate',
value: consolestring
}
if(datastore.hasOwnProperty(channel)){
datastore[channel].clients.forEach(function(client){
client.send(JSON.stringify(message));
});
}
}

function formatConsoleEntry(req){

var toreturn = '<h5>'+req.method+' '+req.url+'</h5>';
if(req.hasOwnProperty('rawBody')){
toreturn += '<h5>Post Body:</h5>';
toreturn += ('<p>'+req.rawBody+'</p>');
}
toreturn += '<h5>Headers:</h5>';
objectMap(req.headers, function(key, value){
toreturn += ("<span class='key'>"+key+":</span> " + " <span class='value'>"+value+"</span><br>");
});

toreturn += '<hr>';

return toreturn;

}

//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){
throw new Error('This is a 500 Error');
});

//The 404 Route (ALWAYS Keep this as the last route)
server.get('/*', function(req, res){
throw new NotFound;
});

function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}

function objectMap(obj, callback){
var toreturn = [];
for(key in obj){
toreturn.push(callback(key, obj[key]));
}
return toreturn;
}



//A Route for Creating a 500 Error (Useful to keep around) //A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){ server.get('/500', function(req, res){
Expand Down
130 changes: 130 additions & 0 deletions static/css/style.css
Expand Up @@ -106,6 +106,136 @@ textarea {
========================================================================== */ ========================================================================== */




/* Primary Styles
Author: Rob Righter
*/



body{
background-color: #161616;
}

h1, h2 {
font-family: 'Yanone Kaffeesatz', arial, serif;
font-weight: bold;
color: #8B77FF;
}

h1 {
font-size: 50px;
}


h2 {
font-size: 30px;
color: #78B0FF;
}

h5 {
color: #78B0FF;
margin-top: 10px;
}

#main {
width: 950px;
margin-left: auto;
margin-right: auto;
background-color: #111111;
padding: 20px;
}

.instructions {
padding: 10px;
color: #938CC3;
}

.label {
color: #938CC3;
}

#console, #response form textarea {
background-color: #282634;
padding: 10px;
color: #938CC3;
border: 1px solid #382644;
height: 400px;
width: 928px;
margin-top: 10px;
margin-bottom: 55px;
font-family: 'Courier New','Droid Sans Mono', arial, serif;
}

#console {
overflow-y: scroll;
}

#response form button, #homesearch form button {
float: right;
background-color: #75AA3E;
color: #161A1D;
width: 200px;
height: 25px;
border: 1px solid #9ED258;
font-family: 'Yanone Kaffeesatz', arial, serif;
font-size: 18px;
margin-top: 10px;
}

#response form button:hover, #homesearch form button:hover {
background-color: #A7D765;
}

code {
font-family: 'Courier New','Droid Sans Mono', arial, serif;
}

#homesearch form #channelselector {
background-color: #282634;
padding: 5px;
color: #938CC3;
border: 1px solid #382644;
width: 490px;
font-size: 25px;
font-family: 'Courier New','Droid Sans Mono', arial, serif;
}

#homesearch {
width: 500px;
margin-left: auto;
margin-right: auto;
padding-bottom: 150px;
}

#homesearch h2, #homesearch .label {
float: left;
}

#homesearch h1 {
margin-bottom: 40px;
}

#homesearch .label {
margin-top: 17px;
margin-left: 10px;
}

.break {
clear: both;
}

#console .key {
color: #8C6DFF;
}

hr {
color:none;
border:none;
border-top:1px #290943 solid;
height:1px;
overflow:hidden;
line-height:1px;
}






Expand Down
37 changes: 31 additions & 6 deletions static/js/script.js
Expand Up @@ -6,10 +6,35 @@ $(document).ready(function() {
var socket = io.connect(); var socket = io.connect();


$('#sender').bind('click', function() { $('#sender').bind('click', function() {
socket.emit('message', 'Message Sent on ' + new Date()); socket.send("Message Sent on " + new Date());
}); });


socket.on('server_message', function(data){ socket.on('message', function(data){
$('#receiver').append('<li>' + data + '</li>'); var message = JSON.parse(data);
});
switch(message.cmd){
case 'consoleupdate':
//do something
$('#console').prepend(message.value);
break;
default:
//do something
break;
}
//$('#reciever').append('<li>' + data + '</li>');
});

$('#homesearch form').submit(function(){
window.location = '/' + $('#channelselector').val() + "/console";
return false;
});

$('#response form button').click(function(){
socket.send(JSON.stringify({
cmd : "saveresponse",
responsetext : $('#response form textarea').val(),
channel : channelname
}));
return false;
})
}); });
25 changes: 25 additions & 0 deletions views/console.jade
@@ -0,0 +1,25 @@
extends layout

block content
h1='Request Inspector'
h2=channelname

div.instructions
p='To view the request to a POST or a GET on this console, direct your request to the following url:'
code='http://robrighter.no.de/'+channelname

h2='Console:'
#console=''

#response
form
button(type='button')='Save Response'
h2='Response Text (for both POST and GET):'
textarea=responsetext
script
| var channelname = '<%= channelname %>';
| socket.send(JSON.stringify({
| cmd : "connect",
| channel : channelname
| }));

0 comments on commit c148e74

Please sign in to comment.