- Home /
jslib from WebSocket doesn't receive data
I'm trying to use the websocket package unity provides from the asset store to connect to my server from WebGL, but for some reason I cannot receive messages; only send them when testing in the browers , but works fine in the editor. I've found the reason it works in the editor is because in all other platforms except WebGL, it uses websocket-sharp.dll, and in WebGL it replaces the dll for a jslib.
The client WebSocket wrapper
The client echo script
The server code
Any help appreciated, thanks!
Answer by EarthLaunch · Jan 03, 2016 at 09:26 AM
Latest Nov 2015 version is missing string receive handling (and a warning about uncaught types...), to hack fix it add this in WebSocket.jslib around line 30, tacked onto the if/else for onmessage handling:
else if(typeof e.data === "string") {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
var array = new Uint8Array(reader.result);
socket.messages.push(array);
});
var blob = new Blob([e.data]);
reader.readAsArrayBuffer(blob);
}
Credit goes to https://github.com/sta/websocket-sharp/issues/181#issuecomment-158873401
Answer by WilliamLeu · Feb 09, 2018 at 08:22 PM
EarthLaunch's answer works and it helped me out, but I had a problem that took me a while to figure out, FileReader is asynchronous - and there's really no synchronous/blocking way to do the same code.
Why does that matter? Because large messages that were sent earlier took longer to process, and messages were being processed out of order. This matters if you're sending messages to create objects, and messages to modify and process those objects, and then you're trying to modify objects before the webapp even created them and your app breaks (even though it works fine in Unity... best type of bug...).
So to anyone who needs assurance that the messages are processed in order, I added another modification. If there's a more elegant way to make sure messages are processed in order, I'd love to know.
At the top, two new variables need to be added, an array for the order of buffered reads, and a set for those that are finished.
SocketCreate: function(url)
{
var str = Pointer_stringify(url);
var socket = {
socket: new WebSocket(str),
buffer: new Uint8Array(0),
error: null,
messages: [],
readerqueue: [],
finishedreads : new Set()
}
And every time we finish reading, we log ourselves as finished and flush the items in order that are finished.
else if(typeof e.data === "string")
{
var reader = new FileReader();
reader.addEventListener(
"loadend",
function()
{
socket.finishedreads.add(reader);
// Make sure messages are added in order.
// For reasons of javascript etiquete, we couldn't
// have
while(socket.readerqueue.length != 0)
{
var topItem = socket.readerqueue[0];
if( socket.finishedreads.has(topItem))
{
var array = new Uint8Array(topItem.result);
socket.messages.push(array);
socket.readerqueue.shift();
socket.finishedreads.delete(topItem);
}
else
break;
}
});
var blob = new Blob([e.data]);
socket.readerqueue.push(reader)
reader.readAsArrayBuffer(blob);
}
Your answer
Follow this Question
Related Questions
Browser interaction to Unity 0 Answers
What headers can be passed through in web gl 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity3d Csharp Networking Error 3 Answers