2017-11-02 04:50:56 +01:00
|
|
|
<input id="input" type="text" />
|
|
|
|
<button onclick="send()">Send</button>
|
|
|
|
<pre id="output"></pre>
|
|
|
|
<script src="/iris-ws.js"></script>
|
|
|
|
<script>
|
|
|
|
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
|
|
|
|
var port = document.location.port ? (":" + document.location.port) : "";
|
|
|
|
// see app.Get("/echo", ws.Handler()) on main.go
|
|
|
|
var wsURL = scheme + "://" + document.location.hostname + port+"/echo";
|
|
|
|
|
|
|
|
var input = document.getElementById("input");
|
|
|
|
var output = document.getElementById("output");
|
|
|
|
|
|
|
|
// Ws comes from the auto-served '/iris-ws.js'
|
|
|
|
var socket = new Ws(wsURL)
|
|
|
|
socket.OnConnect(function () {
|
|
|
|
output.innerHTML += "Status: Connected\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.OnDisconnect(function () {
|
|
|
|
output.innerHTML += "Status: Disconnected\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
// read events from the server
|
|
|
|
socket.On("chat", function (msg) {
|
|
|
|
addMessage(msg)
|
|
|
|
});
|
|
|
|
|
|
|
|
function send() {
|
|
|
|
addMessage("Me: " + input.value) // write ourselves
|
|
|
|
socket.Emit("chat", input.value);// send chat event data to the websocket server
|
|
|
|
input.value = ""; // clear the input
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMessage(msg) {
|
|
|
|
output.innerHTML += msg + "\n";
|
|
|
|
}
|
|
|
|
|
2017-09-17 13:43:35 +02:00
|
|
|
</script>
|