Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party NEED HELP on websocket sharp

Discussion in 'Multiplayer' started by rzbh, Apr 11, 2021.

  1. rzbh

    rzbh

    Joined:
    Feb 16, 2021
    Posts:
    2
    hi guys

    im trying to create a multiplayer game with dedicated server and now having problem with websocket sharp

    i just want to change a text value on scene when get a message from server and show the message on scene

    please let me know whats wrong with my code

    _____________________________________________________________________________________
    using UnityEngine;
    using WebSocketSharp;
    using UnityEngine.UI;

    public class WsClient : MonoBehaviour
    {
    WebSocket ws;
    string tmpText;

    public TextChanger other;

    JsonObject jj = new JsonObject();


    private void Start()
    {
    other = FindObjectOfType<TextChanger>();

    ws = new WebSocket("ws://localhost:8080");
    ws.OnOpen += (sender, e) =>
    {
    Debug.Log("Connected");

    };

    ws.OnError += (sender, err) =>
    {

    Debug.Log("error " + err.Message);
    Debug.Log("ERR");
    };

    ws.OnMessage += (sender, e) =>
    {

    jj = JsonUtility.FromJson<JsonObject>(e.Data);

    Debug.Log("TOP");
    other.TXTChanger(e.Data);
    Debug.Log("DOWN");
    };


    ws.Connect();


    }

    private void Update()
    {
    if (ws == null)
    {
    return;
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
    ws.Send("Hello");
    }


    }

    }
    ________________________________________________________________________________________


    i see TOP , error message and ERR but Debug.Log("DOWN") didnt executed


    this is error message :

    error An error has occurred during an OnMessage event.
    UnityEngine.Debug:Log (object)
    WsClient/<>c:<Start>b__7_1 (object,WebSocketSharp.ErrorEventArgs) (at Assets/WsClient.cs:27)
    WebSocketSharp.Ext:Emit<WebSocketSharp.ErrorEventArgs> (System.EventHandler`1<WebSocketSharp.ErrorEventArgs>,object,WebSocketSharp.ErrorEventArgs)
    WebSocketSharp.WebSocket:error (string,System.Exception)
    WebSocketSharp.WebSocket:messagec (WebSocketSharp.MessageEventArgs)
    WebSocketSharp.WebSocket:message ()
    WebSocketSharp.WebSocket/<>c__DisplayClass176_0:<startReceiving>b__1 (WebSocketSharp.WebSocketFrame)
    WebSocketSharp.WebSocketFrame/<>c__DisplayClass83_0:<ReadFrameAsync>b__3 (WebSocketSharp.WebSocketFrame)
    WebSocketSharp.WebSocketFrame/<>c__DisplayClass77_0:<readPayloadDataAsync>b__0 (byte[])
    WebSocketSharp.Ext/<>c__DisplayClass48_0:<ReadBytesAsync>b__0 (System.IAsyncResult)
    System.Threading._ThreadPoolWaitCallback:performWaitCallback ()
     
  2. rzbh

    rzbh

    Joined:
    Feb 16, 2021
    Posts:
    2
    no one can help?:(
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The way you've posted your code unformatted makes it extremely difficult to read. Using non-descriptive names for every variable makes it even harder to follow.

    You say you see "TOP", well the code that follows that is in some TextChanger class, which you haven't included. You say you see "ERR" and an error message, well that is in response to your connect request. Is the server running when you try to connect to it? What does the server see when the client tries to connect?

    https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  4. GhadaAJIMI

    GhadaAJIMI

    Joined:
    Nov 23, 2022
    Posts:
    4
    Hi, I have the same problem as you, did you find a solution for it?
     
  5. YousafGrewal

    YousafGrewal

    Joined:
    Jul 5, 2018
    Posts:
    27
  6. LazarovM

    LazarovM

    Joined:
    Jan 14, 2017
    Posts:
    1
    I fixed it using Coroutines

    private void Start()
    {
    StartCoroutine(HandleMessageRespose()); // This is very important
    }


    public void ConnectToLocalServer(Action OnConnected, Action<string> OnError)
    {
    this.OnError = OnError;
    ws = new WebSocket("ws://localhost:8080");
    ws.OnOpen += (sender, e) =>
    {
    Debug.Log("Connected to server");
    };
    ws.OnMessage += (sender, e) =>
    {
    Debug.Log("Received message: " + e.Data);
    // Handle received message
    receivedResponse = true;
    messagesQueue.Add(responseData);
    };
    ws.OnError += (sender, e) =>
    {
    Debug.LogError("WebSocket error: " + e.Message);
    };
    ws.OnClose += (sender, e) =>
    {
    Debug.Log("Disconnected from server");
    };
    ws.Connect();
    }

    private IEnumerator HandleMessageRespose()
    {
    yield return new WaitUntil(() => messagesQueue.Count > 0);
    if (receivedResponse)
    {
    ServerData jsonData = JsonUtility.FromJson<ServerData>(responseData);
    receivedResponse = false;
    ......
    messagesQueue.Clear();

    StartCoroutine(HandleMessageRespose());
    }

    this way the Messages are sent to separate thread and it doesn't crash.