Created
May 23, 2012 18:22
-
-
Save julescarbon/2776828 to your computer and use it in GitHub Desktop.
hangout API test.js
This file contains 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 characters
// Tests state changes and message passing | |
var StateTest = { | |
'_pingTime': 0, | |
'init': function(){ | |
$("#message").keydown(StateTest.send); | |
$("#send").click(function(){ | |
StateTest.send({'keyCode': 13}); | |
}); | |
$("#ping").click(StateTest.ping); | |
$("#get-state").click(StateTest.getState); | |
$("#server-test").click(StateTest.serverMessage); | |
gapi.hangout.data.onStateChanged.add(StateTest.onStateChange); | |
gapi.hangout.data.onMessageReceived.add(StateTest.onMessage); | |
}, | |
// Request some JSON from the server | |
'serverMessage': function () { | |
console.log('Requesting message from main.py'); | |
var http = new XMLHttpRequest(); | |
http.open('GET', serverPath); | |
http.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
var jsonResponse = JSON.parse(http.responseText); | |
console.log(jsonResponse); | |
$("#ping-log").append(jsonResponse['message'] + "<br>" ); | |
} | |
} | |
http.send(); | |
}, | |
// Send a message via shared state | |
'send': function(e){ | |
if (e.keyCode == 13) { | |
var msg = $("#message").val(); | |
console.log('sending: ' + msg); | |
gapi.hangout.data.submitDelta({'msg': msg}); | |
} | |
}, | |
// Dump current state to the console | |
'getState': function(){ | |
var state = gapi.hangout.data.getState(); | |
console.log(state); | |
}, | |
// Display the message when the shared state changes | |
'onStateChange': function(e){ | |
console.log(e); | |
var state = e.state; | |
msg = e.state.msg; | |
$("#receive").text(msg); | |
}, | |
// Send a ping to the other people in the hangout | |
'ping': function() { | |
$("#ping-log").append( "PING<br>" ); | |
StateTest._pingTime = new Date(); | |
gapi.hangout.data.sendMessage("ping"); | |
}, | |
// Receive a ping and send a pong | |
// Receive a pong and display the roundtrip time | |
'onMessage': function(event) { | |
$("#ping-log").append( "onMessage<br>" ); | |
if (event.message == "ping") { | |
gapi.hangout.data.sendMessage("pong"); | |
$("#ping-log").append( "PONG<br>" ); | |
} | |
if (event.message == "pong") { | |
var now = new Date(); | |
var diff = now.getTime() - StateTest._pingTime.getTime(); | |
$("#ping-log").append("ping roundtrip: " + diff + "<br>"); | |
} | |
} | |
}; | |
$(StateTest.init); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment