68 lines
1.2 KiB
HTML
68 lines
1.2 KiB
HTML
|
|
<!DOCTYPE html>
|
||
|
|
<html lang="en">
|
||
|
|
<head>
|
||
|
|
<title>WebSocket Client</title>
|
||
|
|
<style>
|
||
|
|
#output {
|
||
|
|
border: solid 1px #000;
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
|
||
|
|
<form id="form">
|
||
|
|
<input type="text" id="message">
|
||
|
|
<button type="submit">Send</button>
|
||
|
|
</form>
|
||
|
|
|
||
|
|
<hr>
|
||
|
|
|
||
|
|
<div id="output"></div>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
|
||
|
|
var inputBox = document.getElementById("message");
|
||
|
|
var output = document.getElementById("output");
|
||
|
|
var form = document.getElementById("form");
|
||
|
|
|
||
|
|
try {
|
||
|
|
|
||
|
|
var host = "ws://de5.runetki.co:3331";
|
||
|
|
console.log("Host:", host);
|
||
|
|
|
||
|
|
var s = new WebSocket(host);
|
||
|
|
|
||
|
|
s.onopen = function (e) {
|
||
|
|
console.log("Socket opened.");
|
||
|
|
};
|
||
|
|
|
||
|
|
s.onclose = function (e) {
|
||
|
|
console.log("Socket closed.");
|
||
|
|
};
|
||
|
|
|
||
|
|
s.onmessage = function (e) {
|
||
|
|
alert(e.data)
|
||
|
|
console.log("Socket message:", e.data);
|
||
|
|
var p = document.createElement("p");
|
||
|
|
p.innerHTML = e.data;
|
||
|
|
output.appendChild(p);
|
||
|
|
};
|
||
|
|
|
||
|
|
s.onerror = function (e) {
|
||
|
|
console.log("Socket error:", e);
|
||
|
|
};
|
||
|
|
|
||
|
|
} catch (ex) {
|
||
|
|
console.log("Socket exception:", ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
form.addEventListener("submit", function (e) {
|
||
|
|
e.preventDefault();
|
||
|
|
s.send(inputBox.value);
|
||
|
|
inputBox.value = "";
|
||
|
|
}, false)
|
||
|
|
|
||
|
|
</script>
|
||
|
|
|
||
|
|
</body>
|
||
|
|
</html>
|