Skip to content

Instantly share code, notes, and snippets.

@atestu
Created March 25, 2011 17:53
Show Gist options
  • Save atestu/887268 to your computer and use it in GitHub Desktop.
Save atestu/887268 to your computer and use it in GitHub Desktop.
Sends socket.io messages to the server writing on the S3 account using the knox library for node.js
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Alexandre Testu">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://mturk-script.herokuapp.com/socket.io/socket.io.js"></script>
<script type="text/javascript" charset="utf-8">
var socket = io.connect('http://mturk-script.herokuapp.com', {'try multiple transports': true, 'transports': ['flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']});
socket.on('connect', function(){ // this code runs when you connect to the server (that is every time you reload the page)
// sample request to send a json object
// userid is the amazon id, I think we can retreive it easily with jQuery ?
// this is a test with an "info" debugging json object
var writeTest = {type: 'write', userid: 'TEST', json: '{type: \'info\', content: \'teeeessssttt content\'}'};
var ipJSON;
$.ajax({
url: 'http://api.ipinfodb.com/v3/ip-city/?key=bcdc23035f02536c7c289261bdadf08247eb75320fafddda571c4f25c480f207&format=json',
dataType: 'jsonp',
crossDomain: true,
success: function (data) {
socket.json.send({type: 'write', userid: 'asdfasdf',
json: "{countryCode: \"" + data.countryCode
+ "\", ipAddress: \"" + data.ipAddress
+ "\", cityName: \"" + data.cityName + "\"}"});
}
});
// sample request to receive a json object
var readTest = {type: 'read', userid: 'TEST'};
socket.emit('message', {type: 'read', userid: 'TEST'}); //<-- this is how you send a request
});
socket.on('message', function(message){ // this code runs when you receive a message from the server
if (message.type == 'info'){
// this will be use for debugging info
console.log(message.content);
}
else {
// console.log (message);
console.log( eval("(" + message.json + ")") );
}
});
socket.on('disconnect', function(){ // this code runs when you receive are disconnnected from the server (this should never happen)
});
</script>
</head>
<body>
mturk goes here!
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment