Elm WebSocket with binary data
I need to use sockets with binary data, so I copied the Evan library into my source code and hacked my own code to read
socket.addEventListener("message", function(event) {
var reader = new FileReader();
console.log("** onmessage");
try {
reader.readAsArrayBuffer(event.data);
} catch (e) {
console.error(e)
}
reader.addEventListener("loadend", function(event) {
// ArrayBuffer
let record = msgpack.decode(new Uint8Array(event.target.result));
let recordString = JSON.stringify(record.NewRecords[0].Record);
_elm_lang$core$Native_Scheduler.rawSpawn(A2(settings.onMessage, socket, recordString));
});
});
I have three errors:
- I am getting fewer callbacks in Elm than
** onmessage
logs - for large messages i get a fraction of the logs
** onmessage
i expect - I am not receiving any follow-up messages
First, I thought that my closure above would make a new instance of filereader every time the message appears (and indeed, I don't get any errors that filerader is busy). Code like this worked fine when I built a simple js socket handling code connected to a port.
Second, it looks like the socket has completely lost messages. I am wondering if there is a problem with having _elm_lang$core$Native_Scheduler.rawSpawn(A2(settings.onMessage, socket, recordString));
a closure in the new that leads to some kind of race condition on the value socket
?
Update
Using the dev console, I see that the first time I hit the rawSpawn line, socket.readyState == 1
(open) and then socket.readyState == 3
(i.e. Closed). I don't have any code that closes it.
source to share
No one has answered this question yet
Check out similar questions: