Over my websocket I expect a Binary message from the server to the client (the other way works). I want to cast that to an Array[Byte]
to further process it it as protobuf-message
ws.onmessage = {
(event: MessageEvent) =>
val msg = event.data.toString
dom.window.console.log(msg)
val x = event.data.asInstanceOf[ArrayBuffer]
val df = new DataView(x)
dom.window.console.log("result")
dom.window.console.log(bin2User(df).toString)
}
private def bin2User(data: DataView): User = {
val bytes = new Array[Byte](data.byteLength)
for(index <- 0 to data.byteLength) {
bytes(index) = data.getInt8(index)
}
User.parseFrom(bytes)
}
now dom.window.console.log(msg)
gives me
[object Blob]
Blob { size: 9, type: "" }
I would expect the x
be of type ArrayBuffer
because of the explicit cast, but alas, it's not, as I get
TypeError: DataView: expected ArrayBuffer, got Blob
How can I overcome this?
I tried with:
val fr = new FileReader
fr.readAsArrayBuffer(event.data.asInstanceOf[Blob])
val y = fr.result.asInstanceOf[ArrayBuffer]
dom.window.console.log(y)
but this prints null
for y
and also
TypeError: y is not an object