Browse Source

created observer (new protocol)

master
Joshua Moerman 12 years ago
parent
commit
c9128789c1
  1. 62
      index.html
  2. 39
      observer.html
  3. 46
      websocket.html

62
index.html

@ -0,0 +1,62 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var myName = Math.random().toString(36).substring(7);
var websocket;
var myself = {
"name": myName,
"width": 0,
"height": 0
};
function init() {
document.getElementById("myName").innerHTML = myName;
show_size();
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://127.0.0.1:7681");
websocket.onopen = function (evt) { onOpen(evt) };
websocket.onclose = function (evt) { onClose(evt) };
websocket.onmessage = function (evt) { onMessage(evt) };
websocket.onerror = function (evt) { onError(evt) };
}
function onOpen(evt) {
document.getElementById("status").innerHTML = "CONNECTED";
doSend();
}
function onMessage(evt) {
document.getElementById("people").innerHTML = evt.data;
setTimeout(function(){
doSend();
}, 1000);
}
function doSend(message) {
myself.name = myName;
myself.width = window.innerWidth;
myself.height = window.innerHeight;
websocket.send(JSON.stringify(myself));
}
function onError(evt) {
document.getElementById("status").innerHTML = 'ERROR: ' + evt.data;
}
function onClose(evt) {
document.getElementById("status").innerHTML = "DISCONNECTED";
}
function show_size(){
document.getElementById("reso").innerHTML = window.innerWidth + 'x' + window.innerHeight;
}
window.addEventListener("load", init, false);
window.addEventListener("resize", show_size, false);
</script>
<p>My name is: <span id="myName"></span>
<p>My resoltion is: <span id="reso"></span>
<p>Response of server: <span id="people"></span>
<p>Status: <span id="status">unknown</span>
</html>

39
observer.html

@ -0,0 +1,39 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var websocket;
var list_of_people;
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://127.0.0.1:7681", "observer");
websocket.onopen = function (evt) { onOpen(evt) };
websocket.onclose = function (evt) { onClose(evt) };
websocket.onmessage = function (evt) { onMessage(evt) };
websocket.onerror = function (evt) { onError(evt) };
}
function onOpen(evt) {
document.getElementById("status").innerHTML = "CONNECTED";
// needs to be non-empty
websocket.send("a");
}
function onMessage(evt) {
list_of_people = JSON.parse(evt.data);
}
function onError(evt) {
document.getElementById("status").innerHTML = 'ERROR: ' + evt.data;
}
function onClose(evt) {
document.getElementById("status").innerHTML = "DISCONNECTED";
}
window.addEventListener("load", init, false);
</script>
<p>Response of server: <span id="people"></span>
<p>Status: <span id="status">unknown</span>
</html>

46
websocket.html

@ -1,46 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://127.0.0.1:7681";
var people;
var myName = Math.random().toString(36).substring(7);
function init() {
people = document.getElementById("people");
document.getElementById("myName").innerHTML = myName;
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {onOpen(evt)};
websocket.onclose = function (evt) {onClose(evt)};
websocket.onmessage = function (evt) {onMessage(evt)};
websocket.onerror = function (evt) {onError(evt)};
}
function onOpen(evt) {
doSend(myName);
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
writeToScreen(evt.data);
setTimeout(function(){doSend(myName);}, 1000);
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) {
websocket.send(message);
}
function writeToScreen(message) {
people.innerHTML = message;
}
window.addEventListener("load", init, false);
</script>
<p>My name is: <span id="myName"></span>
<p>Response of server: <span id="people"></span>
</html>