NEED HELP on websocket sharp

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();

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(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: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:b__1 (WebSocketSharp.WebSocketFrame)
WebSocketSharp.WebSocketFrame/<>c__DisplayClass83_0:b__3 (WebSocketSharp.WebSocketFrame)
WebSocketSharp.WebSocketFrame/<>c__DisplayClass77_0:b__0 (byte[ ])
WebSocketSharp.Ext/<>c__DisplayClass48_0:b__0 (System.IAsyncResult)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

no one can help?:frowning:

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://discussions.unity.com/t/481379

Hi, I have the same problem as you, did you find a solution for it?

???

I fixed it using Coroutines

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

public void ConnectToLocalServer(Action OnConnected, Action 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(responseData);
receivedResponse = false;

messagesQueue.Clear();

StartCoroutine(HandleMessageRespose());
}

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