|
|
|
<!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>
|