Skip to content

Instantly share code, notes, and snippets.

@joshkehn
Created August 5, 2011 15:01
Show Gist options
  • Save joshkehn/1127715 to your computer and use it in GitHub Desktop.
Save joshkehn/1127715 to your computer and use it in GitHub Desktop.
WebSocket - Part 3
var ws = new WebSocket("ws://localhost:40132");
ws.onopen = function()
{
output("Opened connection.");
};
ws.onmessage = function(e)
{
date = new Date(e.data);
if(isNaN(date.getHours()))
{
output(e.data);
}
else
{
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ap = "AM";
if(hours > 12)
{
hours = hours - 12;
ap = "PM";
}
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
output("Currently " + hours + ":" + minutes + ":" + seconds + " " + ap);
}
};
ws.onclose = function()
{
output("Closed connection.");
};
ws.onerror = function()
{
output("Error occurred.");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment