forked from KxSystems/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.htm
28 lines (28 loc) · 1.04 KB
/
ws.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html><body><title>websocket</title>
<form action="">
<input type="button" value="connect" onclick="connect()" >
<input type="text" id="x" placeholder="q)" >
<input type="submit" value="send" onclick="return send()">
<input type="button" value="close" onclick="ws.close()" >
</form>
<textarea id="out" rows=25 cols=80></textarea>
<script>
var ws,out=document.getElementById("out");
function connect()
{if ("WebSocket" in window)
{var l = window.location;ws = new WebSocket("ws://" + (l.hostname ? l.hostname : "localhost") + ":" + (l.port ? l.port : "5000") + "/");
out.value="connecting..." ;
ws.onopen=function(e){out.value="connected";}
ws.onclose=function(e){out.value="disconnected";}
ws.onmessage=function(e){out.value=e.data;}
ws.onerror=function(e){out.value=e.data;}
}else alert("WebSockets not supported on your browser.");
}
function send()
{x=document.getElementById("x");
v=x.value;
ws.send(v);
out.value="sent "+v;
return false;
}
</script></body></html>