Created
October 4, 2017 17:08
-
-
Save amantix/8579d27d5e0397073b147c569f0949c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script> | |
function WebSocketTest() | |
{ | |
if ("WebSocket" in window) { | |
report("WebSocket поддерживается вашим браузером!"); | |
// Открыть WebSocket | |
var ws = new WebSocket("ws://localhost:8080"); | |
ws.onopen = function () { | |
// WebSocket отправляет данные с помощью send() | |
ws.send(document.getElementById("txt").value); | |
report('Сообщение отправляется...'); | |
}; | |
ws.onmessage = function (evt) { | |
var received_msg = evt.data; | |
report(received_msg); | |
}; | |
ws.onclose = function () { | |
// WebSocket отключен | |
report("Соединение отключено..."); | |
}; | |
} | |
else { | |
// Браузер не поддерживает WebSocket | |
alert("WebSocket не поддерживается вашим браузером!"); | |
} | |
} | |
var report = function (msg) { | |
var info = document.createElement('p'); | |
info.textContent = msg; | |
document.getElementById("sse").appendChild(info); | |
} | |
</script> | |
</head> | |
<body> | |
<input type="text" id="txt"/> | |
<input type="button" value="Test" onclick="WebSocketTest()"/> | |
<div id="sse"/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment