llsouder
04/06/2020, 4:23 PMllsouder
04/06/2020, 4:25 PMCasey Brooks
04/06/2020, 4:31 PMCasey Brooks
04/06/2020, 4:31 PMfun main() {
connect()
}
fun connect() {
// First we create the socket.
// The socket will be connected automatically asap. Not now but after returning to the event loop,
// so we can register handlers safely before the connection is performed.
console.log("Begin connect")
if (window.location.protocol == "https:") {
socket = WebSocket("wss://" + window.location.host + "/ws")
} else {
socket = WebSocket("ws://" + window.location.host + "/ws")
}
// We set a handler that will be executed if the socket has any kind of unexpected error.
// Since this is a just sample, we only report it at the console instead of making more complex things.
socket!!.onerror = {
}
// We set a handler upon connection.
// What this does is to put a text in the messages container notifying about this event.
socket!!.onopen = {
}
// If the connection was closed gracefully (either normally or with a reason from the server),
// we have this handler to notify to the user via the messages container.
// Also we will retry a connection after 5 seconds.
socket!!.onclose = { ev ->
val evt = ev.asDynamic()
// Try to gather an explanation about why this was closed.
var explanation = ""
if (evt.reason && evt.reason.length > 0) {
explanation = "reason: " + evt.reason
} else {
explanation = "without a reason specified"
}
// Notify the user using the messages container.
handleTextMessage("info", "Disconnected with close code " + evt.code + " and " + explanation)
// Try to reconnect after 5 seconds.
window.setTimeout({ connect() }, 5000)
}
// If we receive a message from the server, we want to handle it.
socket!!.onmessage = { ev ->
}
}
llsouder
04/06/2020, 4:33 PMRobert Jaros
04/06/2020, 4:42 PMllsouder
04/06/2020, 4:48 PMllsouder
04/06/2020, 4:54 PMRobert Jaros
04/06/2020, 5:59 PMRobert Jaros
04/06/2020, 6:01 PM