none
Function wait until thread(websocket) to finish before returning result RRS feed

  • Question

  • Hello, I am trying to run a Websocket client example:
    the lib used is: https://github.com/sta/websocket-sharp

    I have made below function to communicate with an Websocket server, the problem is that it returns the result before it is set by the OnMessage function from websocket(ws):
    public static Tuple<int, string> wingm(string userName, string extrastr, string)
            {
                var ws = new WebSocket("wss://just.mywssgame.uk:7002/");
                using (ws)
                {
                    //ws.Connect();
    
                    String returnResultJSON = "";
                    ws.OnMessage += (sender, e) =>
                    {
                        dynamic messageJSONresponse = JsonConvert.DeserializeObject(e.Data);
                        if (Convert.ToString(messageJSONresponse.type) == "playerone")
                            {
    			    returnResultJSON = Convert.ToString(messageJSONresponse);
                                Console.WriteLine(returnResultJSON);
                            }
    
                        }
                    };
                    //we begin here
                    ws.Connect();
                    ws.Send(userName);
                    Console.ReadKey(true);
                }
           return Tuple.Create("0", returnResultJSON);
     }

    I then call above function in an console application:
    Tuple<int, string> tempResult = wingm("somnam", "");
    Console.WriteLine(tempResult.Item1.ToString());
    Console.WriteLine(tempResult.Item2.ToString());
    The problem is that, the function returns the returnResultJSON before it is set by ws.OnMessage, is there a way to wait for the websocket until it is closed? what would the approach be in this situation?

    Thank you in advance, I have been stuck in this for hours.
    Sunday, February 24, 2019 10:58 PM

Answers

  • Hi Miwse,

    Due to I could not test the code, I make some suggestions. Please have a try. There are some complier errors. Hence I am not sure it could be build correctly.

    I blod the changes.

     String returnResultJSON = "";
            public async Task<Tuple<int, string>> Wingm(string userName, string extrastr, string s)
            {
                var ws = new WebSocket("wss://just.mywssgame.uk:7002/");
                using (ws)
                {
                    //ws.Connect();
                   
                    await ws.OnMessage += (sender, e) =>
                    {
                        dynamic messageJSONresponse = JsonConvert.DeserializeObject(e.Data);
                        if (Convert.ToString(messageJSONresponse.type) == "playerone")
                        {
                            returnResultJSON = Convert.ToString(messageJSONresponse);
                            Console.WriteLine(returnResultJSON);
                        }
    
                    };
                };
                //we begin here
                ws.Connect();
                ws.Send(userName);
                Console.ReadKey(true);
                return Tuple.Create(0, returnResultJSON);
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, February 26, 2019 9:29 AM
    • Marked as answer by Miwse Saturday, March 2, 2019 7:58 PM
    Tuesday, February 26, 2019 3:11 AM
    Moderator

All replies

  • Hi Miwse,

    Thank you for posting here.

    >>The problem is that, the function returns the returnResultJSON before it is set by ws.OnMessage,

    For your question, you could try to use async await. Please try the code below.

    public static async Tuple<int, string> wingm(string userName, string extrastr, string)
            {
                var ws = new WebSocket("wss://just.mywssgame.uk:7002/");
                using (ws)
                {
                    //ws.Connect();
    
                    String returnResultJSON = "";
                   await ws.OnMessage += (sender, e) =>
                    {
                        dynamic messageJSONresponse = JsonConvert.DeserializeObject(e.Data);
                        if (Convert.ToString(messageJSONresponse.type) == "playerone")
                            {
    			    returnResultJSON = Convert.ToString(messageJSONresponse);
                                Console.WriteLine(returnResultJSON);
                            }
    
                        }
                    };
                    //we begin here
                    ws.Connect();
                    ws.Send(userName);
                    Console.ReadKey(true);
                }
           return Tuple.Create("0", returnResultJSON);
     }
    >>is there a way to wait for the websocket until it is closed?

    I do not find any close method in the code. The async await also works.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 25, 2019 5:49 AM
    Moderator
  • above suggestions, leaves me with following errors:
    - The return type of an async method must be void, Task or Task<T>
    The event 'WebSocket.OnMessage' can only appear on the left hand side of += or -=

    Any suggestions to fix above errors?

    thank you.

    Monday, February 25, 2019 8:10 PM
  • Hi Miwse,

    Due to I could not test the code, I make some suggestions. Please have a try. There are some complier errors. Hence I am not sure it could be build correctly.

    I blod the changes.

     String returnResultJSON = "";
            public async Task<Tuple<int, string>> Wingm(string userName, string extrastr, string s)
            {
                var ws = new WebSocket("wss://just.mywssgame.uk:7002/");
                using (ws)
                {
                    //ws.Connect();
                   
                    await ws.OnMessage += (sender, e) =>
                    {
                        dynamic messageJSONresponse = JsonConvert.DeserializeObject(e.Data);
                        if (Convert.ToString(messageJSONresponse.type) == "playerone")
                        {
                            returnResultJSON = Convert.ToString(messageJSONresponse);
                            Console.WriteLine(returnResultJSON);
                        }
    
                    };
                };
                //we begin here
                ws.Connect();
                ws.Send(userName);
                Console.ReadKey(true);
                return Tuple.Create(0, returnResultJSON);
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, February 26, 2019 9:29 AM
    • Marked as answer by Miwse Saturday, March 2, 2019 7:58 PM
    Tuesday, February 26, 2019 3:11 AM
    Moderator