Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Test out some websockets stuff, reconnect on connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
iotku committed May 18, 2015
1 parent f109838 commit 4090852
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
update-ghpages.sh
js-launcher.html
websockets-server/websocketd.exe
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ <h3 class="menu-section-title">Docs</h3>
<input type="button" value="Skip Split" onclick="t.skipSplit()"/>
<input type="button" value="Reset" onclick="t.reset()"/>
<input type="button" value="Pause" onclick="t.pause()"/><br>
<input type="button" value="Sendtest" onclick="websock.sendTest()"/><br>

</div>
</div>
</main>
Expand Down
34 changes: 34 additions & 0 deletions js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@
// Shoutouts to him, I probably couldn't have built everything from scratch
// - iotku

function webSocket(f){
var websocketURL = 'ws://localhost:8080/';

console.log(t.maxSplits)
ws = new WebSocket(websocketURL);

ws.onopen = function() {
console.log("WebSockets: Connected to " + websocketURL)
}

this.sendTest = function (start) {
ws.send(t.maxSplits);
console.log("firing")
}

ws.onmessage = function(event) {
console.log(event.data);
};

ws.onerror = function (error) {
console.log('WebSocket Error ' + error.toString());
};
ws.onclose = function(){
//try to reconnect in 5 seconds
console.log("Connection lost! Retrying in 5s.")
setTimeout(function(){webSocket();}, 5000);}
var self = this,
d = d || {}; // I really don't know about this.
}

function GameTimer(d) {
/* User configurable settings */
this.maxSplits = 10; // Max splits to display at once
Expand Down Expand Up @@ -805,6 +835,9 @@ t = new GameTimer({
ms: [2, 1]
});

var websock;
websock = new webSocket();

// Hotkeys. onkeydown is more responsive than onkeyup
window.onkeydown = function keyPress(e) {
var k = e.which || e.keyCode;
Expand All @@ -818,6 +851,7 @@ window.onkeydown = function keyPress(e) {
};

window.onload = function () {
// websock
t.startSplits();
};

Expand Down
13 changes: 13 additions & 0 deletions websockets-server/websockets-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python
from sys import stdout
from sys import stdin
from time import sleep

for count in range(0, 5):
print(count + 1)
stdout.flush()
sleep(1)

while True:
for line in stdin:
print line

0 comments on commit 4090852

Please sign in to comment.