我想創建一個全球websocket參考,我想稍後擴展到自動重新連接。Scala.js全球WebSocket - > InvalidStateError
但是在這裏做嬰兒的步驟我不知道這是爲什麼不工作按預期:
val uri = s"ws://localhost:1337/websocket"
var websocket: Future[WebSocket] = initWebSocket()
def initWebSocket() : Future[WebSocket] = {
val ws = new WebSocket(uri)
ws.onopen =
{
(event: Event) => log("Websocket connection established")
}
ws.onmessage =
{
(event: MessageEvent) => log("Message received")
}
ws.onclose =
{
// TODO wait some seconds before trying to reconnect
(event: CloseEvent) =>
log("Close event received")
// this.websocket = new WebSocket(uri)
}
Future(ws)
}
我完全知道,(草圖)onclose
東西是絕對的廢話,現在!
def main(): Unit = {
websocket.onComplete {
case Failure(x) => println("Epic fail!")
case Success(x) => {
x.send("Test")
}
}
}
我得到的是
scala.scalajs.js.JavaScriptException: InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
Websocket connection established
The connection to ws://localhost:1337/websocket was interrupted while the page was loading.
Close event received
而且我不甚至不知道爲什麼!
onopen
被稱爲太遲......但如何優雅地解決這個問題?我基本上需要確保在返回websocket之前調用ws.onopen
。我寧願不要阻止,而是使用一些Promise
或Future
完美無缺,謝謝! – Sorona