Skip to content

Instantly share code, notes, and snippets.

@elyphas
Created April 19, 2019 02:31
Show Gist options
  • Save elyphas/a752d306d7b792d50496b3209ca5571a to your computer and use it in GitHub Desktop.
Save elyphas/a752d306d7b792d50496b3209ca5571a to your computer and use it in GitHub Desktop.
object WSChat {
var wsChat = WebSocket ("ws://192.168.0.5:9000/wschat")
wsChat.ws.onopen = { v =>
println("Al abrir la conexion")
val bytes = Pickle.intoBytes[ChatEvent](ChatMessage("Hola")).arrayBuffer()
wsChat.ws.send(bytes)
}
wsChat.ws.binaryType = "arraybuffer"
val blobReader: FileReader = {
//Este es el FileReader, andas perdido.
val reader = new FileReader()
reader.onerror = (e: Event) => {
dom.console.log(s"Error in blobReader: ${reader.error}")
}
reader.onload = (e: UIEvent) => {
reader.result match {
case buf: ArrayBuffer =>
val intP = dom.document.getElementById("int-value")
/*Unpickle[CRUDDetailsRequisicion].fromBytes(TypedArrayBuffer.wrap(buf)) match {
case IntMsgOut(int) =>
intP.innerHTML = int.toString
case ErrorMsgOut(msg) =>
intP.innerHTML = s"ERROR: $msg"
}*/
case _ => // ignored
}
}
reader
}
def onMoveNext(elem: MessageEvent) = {
println ( s"O-->$elem" )
elem.data match {
case buf: ArrayBuffer =>
Unpickle[ChatEvent].fromBytes(TypedArrayBuffer.wrap(buf)) match {
case b: BroadCastMessage =>
println("Recibiendo los mensage Broadcast: " + b)
store.onNext(AddComment(b))
case bFile: BroadCastFile =>
val hashedSig = bFile.file.toBase64
store.onNext(AddComment(bFile))
case m => println("Otro mensage que regresa del servidor!!!!!!!!!!!!!")
}
case blob: Blob =>
dom.console.log("Error on receive, should be a blob.")
blobReader.readAsArrayBuffer(blob)
case msg: String =>
val msgBack = dom.document.getElementById("msgBack").asInstanceOf[html.Span]
msgBack.innerHTML = msg
case _ => dom.console.log("Error on receive, should be a blob." )
}
Continue
}
val catchEvents = new Observer[MessageEvent] {
def onNext(elem: MessageEvent): Future[Ack] = {
println ( s"O-->$elem" )
elem.data match {
case buf: ArrayBuffer =>
Unpickle[ChatEvent].fromBytes(TypedArrayBuffer.wrap(buf)) match {
case b: BroadCastMessage =>
println("Recibiendo los mensage Broadcast: " + b)
store.onNext(AddComment(b))
case bFile: BroadCastFile =>
val hashedSig = bFile.file.toBase64
store.onNext(AddComment(bFile))
case m => println("Otro mensage que regresa del servidor!!!!!!!!!!!!!")
}
case blob: Blob =>
dom.console.log("Error on receive, should be a blob.")
blobReader.readAsArrayBuffer(blob)
case msg: String =>
val msgBack = dom.document.getElementById("msgBack").asInstanceOf[html.Span]
msgBack.innerHTML = msg
case _ => dom.console.log("Error on receive, should be a blob." )
}
Continue
}
def onError(ex: Throwable): Unit = {
ex.printStackTrace()
getBackFromDeath()
}
def onComplete(): Unit = println( "O completed" )
}
def getBackFromDeath():Unit = {
if ( wsChat.ws.readyState == 3) {
wsChat = null
wsChat = WebSocket("ws://192.168.0.5:9000/wschat")
wsChat.ws.binaryType = "arraybuffer"
wsChat.observable.subscribe(catchEvents)
wsChat.ws.onopen = { v =>
val bytes = Pickle.intoBytes[ChatEvent](ChatMessage("Hola")).arrayBuffer()
wsChat.ws.send(bytes)
}
} else wsChat.ws.close()
()
}
wsChat.observable.subscribe(catchEvents)
wsChat.observable.map { v =>
println(v.data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment