Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Websockets can't receive messages from server?

Discussion in 'WebGL' started by mewzi, Aug 29, 2015.

  1. mewzi

    mewzi

    Joined:
    May 28, 2015
    Posts:
    2
    I'm using the latest version of Unity (5.1.3f1) and Simple Web Sockets for Unity WebGL, located on the asset store (https://www.assetstore.unity3d.com/en/#!/content/38367), to test websocket communication.

    In editor, my very simple echo script works just fine. It's able to send and receive messages, as well as display errors, no problems whatsoever. However, when I build and deploy to my test box, I can no longer receive messages from the server. Client is still sending messages and handling errors just fine, and the server's getting those messages and still trying to respond. The client's just a dead fish when it comes to RecvString().

    FWIW, the communication between page and native javascript websocket implementation is fine (I hastily scripted a test to make sure).

    I'm using Firefox Developer Version, but also tested with standard, as far as browsers are concerned.
     
  2. bitdigger

    bitdigger

    Joined:
    Nov 8, 2015
    Posts:
    2
    I think I've found a solution to this that worked at least for my issue, I looked at the C# script in the plugins folder called WebSocket and swapped out the line
    returnEncoding.UTF8.GetString (retval);
    with
    returnConvert.ToBase64String(retval);
    enabled me to apply the byte arrays I was sending from my server to textures. My problem is that I can't send simple strings like "1" to my server which is how I get things rolling from the client side. Been stuck on it for days. Interestingly I can get in touch with the echo server and send messages fine too which makes me think it's to do with my server implementation, which uses jetty
     
  3. EarthLaunch

    EarthLaunch

    Joined:
    Mar 7, 2012
    Posts:
    62
    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:
    Code (CSharp):
    1.         else if(typeof e.data === "string") {
    2.             var reader = new FileReader();
    3.             reader.addEventListener("loadend", function() {
    4.                 var array = new Uint8Array(reader.result);
    5.                 socket.messages.push(array);
    6.             });
    7.             var blob = new Blob([e.data]);
    8.             reader.readAsArrayBuffer(blob);
    9.         }
    Credit goes to https://github.com/sta/websocket-sharp/issues/181#issuecomment-158873401
     
    nttrung143, yuliyF and diekeure like this.