Skip to content

Commit

Permalink
added basic connection checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwohl committed Apr 11, 2015
1 parent 924561c commit cb50548
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions static/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@
<body>
<script type="text/javascript" src="/js/libs/jquery-2.1.3.js"></script>
<script type="text/javascript">
var $body = $('body');
var $body = $('body'),
connected = false,
domain,
user;

$.getJSON('http://localhost:7000/config', function(resp) {
// resp should contain the username and root_domain
connect_ws(resp.root_domain, resp.username);
domain = resp.root_domain;
user = resp.username;
setInterval(check_connection, 5000);
check_connection();
});

function check_connection() {
if (!connected) {
connect_ws(domain, user);
}
}

// initializes WebSocket connection
function connect_ws(root_domain, username) {
console.log('init WebSocket connection as ' + username);
var ws = new WebSocket("ws://" + root_domain + "/ws/" + username);
ws.onopen = function() {
console.log('connection open');
ws.send("connecting as " + username);
connected = true;
};
ws.onmessage = function(evt) {
var data = JSON.parse(evt.data);
Expand All @@ -57,7 +71,10 @@
}, 1000);
});
}

};
ws.onclose = function(evt) {
console.log('connection closed');
connected = false;
};
}
</script>
Expand Down

0 comments on commit cb50548

Please sign in to comment.