Simple Web Socket Not Receiving Message in WebGL


I am using the Simple Websockets package, which an be found here:

I am using it to send and receive messages over websockets. While running my project in the Unity editor, I am able to successfully connect to my server, send a message, and receive messages. But, when I build the project for WebGL, and load it in Firefox, it is able to connect and send a message (I see that the message is received on my server), but it is not receiving messages back.

I am using the code from their example project (which, by the way, does work in WebGL - it is successfully sending and receiving messages and printing them to the console).

 w = new WebSocket(new Uri("ws://"));
        yield return StartCoroutine(w.Connect());
        while (true)
            string reply = w.RecvString();
            if (reply != null)
                Debug.Log("Received: " + reply);
                Debug.Log("Empty Reply." + reply);
            if (w.error != null)
                Debug.LogError("Error: " + w.error);
            yield return 0;

I can see why it is happening - the code for receiving messages is below:

    public string RecvString()
        byte[] retval = Recv();
        if (retval == null)
            return null;
        return Encoding.UTF8.GetString(retval);

	public byte[] Recv()
		int length = SocketRecvLength (m_NativeRef);
		if (length == 0)
			return null;
		byte[] buffer = new byte[length];
		SocketRecv (m_NativeRef, buffer, length);
		return buffer;

The length returned by Recv is always 0, which causes null to be returned and I do not receive any messages.

As I mentioned above, this works perfectly when I run it in the Unity Editor and even in the WebGL build I can see my client connect to the server and subscribe to the topic that I am expecting it to receive messages on.

Any help would be really appreciated.

Managed to answer my own question! The answer can be found here:

With credit being given here:

I will post the answer here too, in case either or both of those links gets removed.

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 === "string") {
            var reader = new FileReader();
            reader.addEventListener("loadend", function() {
                var array = new Uint8Array(reader.result);
            var blob = new Blob([]);