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.

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,824
    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?