summaryrefslogtreecommitdiff
path: root/cask/util/src-js/cask/util/WebsocketClientImpl.scala
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.li@databricks.com>2019-09-16 22:31:03 +0800
committerLi Haoyi <haoyi.li@databricks.com>2019-09-16 22:31:03 +0800
commitbfe26d5a9705011359658c45b364e9b65ce697b5 (patch)
tree656565b3d147b1837d4cfc1d949e7af8c250f16c /cask/util/src-js/cask/util/WebsocketClientImpl.scala
parent84ea971b1261919aca7b31635ddc7d0dca830fea (diff)
downloadcask-bfe26d5a9705011359658c45b364e9b65ce697b5.tar.gz
cask-bfe26d5a9705011359658c45b364e9b65ce697b5.tar.bz2
cask-bfe26d5a9705011359658c45b364e9b65ce697b5.zip
Provide a simple builtin websocket client in `cask.WsClient`
Harmonize the actor-based APIs of `cask.WsClient`/`cask.WsHandler`/`cask.WsActor`, letting them share the same set of `cask.Ws` events The default implementation of `cask.WsClient` on the JVM spawns one thread per connection, and doesn't really scale to large numbers of connections. For now we just continue using AsyncHttpClient in the load tests. Wrapping AsyncHttpClient in a nice API is TBD
Diffstat (limited to 'cask/util/src-js/cask/util/WebsocketClientImpl.scala')
-rw-r--r--cask/util/src-js/cask/util/WebsocketClientImpl.scala33
1 files changed, 33 insertions, 0 deletions
diff --git a/cask/util/src-js/cask/util/WebsocketClientImpl.scala b/cask/util/src-js/cask/util/WebsocketClientImpl.scala
new file mode 100644
index 0000000..f129893
--- /dev/null
+++ b/cask/util/src-js/cask/util/WebsocketClientImpl.scala
@@ -0,0 +1,33 @@
+package cask.util
+
+import org.scalajs.dom
+
+abstract class WebsocketClientImpl(url: String) extends WebsocketBase{
+ var websocket: dom.WebSocket = null
+ var closed = false
+ def connect(): Unit = {
+ websocket = new dom.WebSocket(url)
+
+ websocket.onopen = (e: dom.Event) => onOpen()
+ websocket.onmessage = (e: dom.MessageEvent) => onMessage(e.data.asInstanceOf[String])
+ websocket.onclose = (e: dom.CloseEvent) => {
+ closed = true
+ onClose(e.code, e.reason)
+ }
+ websocket.onerror = (e: dom.Event) => onError(new Exception(e.toString))
+ }
+ def onOpen(): Unit
+
+ def send(value: String) = try {
+ websocket.send(value)
+ true
+ } catch{case e: scala.scalajs.js.JavaScriptException => false}
+
+
+ def send(value: Array[Byte]) = ???
+ def onError(ex: Exception): Unit
+ def onMessage(value: String): Unit
+ def onClose(code: Int, reason: String): Unit
+ def close(): Unit = websocket.close()
+ def isClosed() = closed
+} \ No newline at end of file