Browse Source

Merge branch 'websocket-ui'

pull/7/head
Jeff Becker 8 years ago
parent
commit
9ab415da9d
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B
  1. 4
      websocket-ui/index.html
  2. 94
      websocket-ui/ui.js

4
websocket-ui/index.html

@ -31,6 +31,10 @@
</style> </style>
</head> </head>
<body> <body>
<div id="login-wrapper">
<input id="ws-url" value="ws://127.0.0.1:7666"></input>
<button id="connect-button" onclick="startui()">connect</button>
</div>
<pre id="log"></pre> <pre id="log"></pre>
<canvas id="main"> <canvas id="main">
</canvas> </canvas>

94
websocket-ui/ui.js

@ -1,5 +1,3 @@
var url = "ws://127.0.0.1:7665";
var l = document.getElementById("log"); var l = document.getElementById("log");
var c = document.getElementById("main"); var c = document.getElementById("main");
var nodes = { var nodes = {
@ -119,18 +117,16 @@ function tunnelState(tid, state) {
tunnelFailed(tid); tunnelFailed(tid);
} else if (state == "6" ) { } else if (state == "6" ) {
tunnelExpiring(tid); tunnelExpiring(tid);
} else {
logit("tunnel "+tid+" state "+state);
} }
} }
function tunnelCreated(tid) { function tunnelCreated(tid) {
logit("tunnel "+tid+" was created"); logit("Tunnel "+tid+" was created");
} }
function logit(msg) { function logit(msg) {
console.log(msg); console.log(msg);
var t = document.createTextNode(leftpad(msg, 25)); var t = document.createTextNode(msg);
var e = document.createElement("div"); var e = document.createElement("div");
e.appendChild(t); e.appendChild(t);
l.appendChild(e); l.appendChild(e);
@ -138,38 +134,66 @@ function logit(msg) {
l.removeChild(l.children[0]); l.removeChild(l.children[0]);
} }
var ws = new WebSocket(url); function socketClosed() {
ws.onclose = function (ev) { var b = document.getElementById("connect-button");
nodes = { length: 0 }; b.onclick = startui
tpeers = { length: 0 }; b.innerHTML = "connect";
} }
ws.onmessage = function(ev) {
var j = JSON.parse(ev.data); function startui() {
if (j) { var el = document.getElementById("ws-url");
console.log(j); var url;
if(j.type == "transport.connected") { if(el)
nodeConnected(j.ident); url = el.value;
} else if (j.type == "transport.disconnected") { else
nodeDisconnected(j.ident); url = "ws://127.0.0.1:7666";
} else if (j.type == "transport.sendmsg") {
nodeSend(j.ident, j.number); var ws = new WebSocket(url);
} else if (j.type == "transport.recvmsg") { ws.onclose = function (ev) {
nodeRecv(j.ident, j.number); if (ev.code == 1000)
} else if (j.type == "tunnel.build") { logit("connection closed");
tunnelBuild(j.value, j.tid, j.inbound); else
} else if (j.type == "tunnel.latency") { logit("failed to connect to "+url);
tunnelLatency(j.tid, j.value); nodes = { length: 0 };
} else if (j.type == "tunnel.state") { tpeers = { length: 0 };
tunnelState(j.tid, j.value); socketClosed();
} else if (j.type == "tunnels.created") { }
tunnelCreated(j.tid); ws.onopen = function(ev) {
} else if (j.type == "tunnels.expired") { logit("connected to "+url);
tunnelExpiring(j.tid); var b = document.getElementById("connect-button");
} else { b.onclick = function() {
logit("message: "+j.type); ws.close();
} }
b.innerHTML = "disconnect";
} }
}; ws.onmessage = function(ev) {
var j = JSON.parse(ev.data);
if (j) {
console.log(j);
if(j.type == "transport.connected") {
nodeConnected(j.ident);
} else if (j.type == "transport.disconnected") {
nodeDisconnected(j.ident);
} else if (j.type == "transport.sendmsg") {
nodeSend(j.ident, j.number);
} else if (j.type == "transport.recvmsg") {
nodeRecv(j.ident, j.number);
} else if (j.type == "tunnel.build") {
tunnelBuild(j.value, j.tid, j.inbound);
} else if (j.type == "tunnel.latency") {
tunnelLatency(j.tid, j.value);
} else if (j.type == "tunnel.state") {
tunnelState(j.tid, j.value);
} else if (j.type == "tunnels.created") {
tunnelCreated(j.tid);
} else if (j.type == "tunnels.expired") {
tunnelExpiring(j.tid);
} else {
logit("message: "+j.type);
}
}
};
}
function getPeer(h, us) { function getPeer(h, us) {
if (tpeers[h]) { if (tpeers[h]) {

Loading…
Cancel
Save