Skip to content

Instantly share code, notes, and snippets.

@amitu
Created March 15, 2012 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitu/2042526 to your computer and use it in GitHub Desktop.
Save amitu/2042526 to your computer and use it in GitHub Desktop.
PingPong HTTP Callbacks base Hammer Application (python using web.py)
<script src="/static/hammerlib.min.js"></script>
<script>
$.fn.on_enter = function (callback) {
var ENTER_KEY = 13;
return this.keypress(function (e){
var key = e.charCode || e.keyCode || 0;
if (key != ENTER_KEY) return;
var value = $(this).val();
if (value === "") return false;
if (!callback(value)) $(this).val("");
return false;
});
}
$(function(){
var connected = false;
hammerlib.bind("hammerlib", "opened", function(data) {
connected = true;
$("#status").text(
"connected [" + hammerlib.get_transport() +
", clientid=" + hammerlib.get_clientid() + "]"
);
});
hammerlib.bind("pingpong", "pong", function(data) {
$("#thedivwrapper").show();
$("#thediv").text(data.text);
});
$('input').focus().on_enter(function(msg){
if(!connected) {
$("#status").text("not connected yet");
}
hammerlib.send("pingpong", "ping", { text: msg });
});
hammerlib.initialize("http://ws.gethammer.co/", "demo.gethammer.co", "user_anon");
});
</script>
import web, hammerlib, logging
logger = logging.getLogger("webpy")
urls = (
'/(.*)', hammerlib.webpy
)
app = web.application(urls, globals())
def pong(request):
request.send_message_to_client(
{
"text": "hey %s, you said: %s" % (
request.remote_ip, request.payload["text"].upper()
)
}, "pong"
)
def dump(request):
logger.info("request: %s", request)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger.info("Starting")
hammerlib.bind("pingpong", "ping", pong)
hammerlib.bind_app("hammerlib", dump)
hammerlib.init_hha("demo.gethammer.co", "<<secret key>>")
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment