sta / sta/websocket-sharp

Request/Response paradigm for Websocketsharp library with using Websocket.SendAsync() and OnMessage callback function.

Open
#403 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

We are trying to implement request/response paradigm for Websocketsharp library exactly same it works for HttpClient's request/response async behavior. We are trying to achieve it using the async callback as given in below code. We tried to get SendAsync method's OnMessage callback event to wait until the server sends the response. We are able to get the response within the scope of the SendAsync method but as soon as we come out of SendAsync scope it clears out the value of the response.

string clientResponse = null;

        var response = Task.Run(() => objWSClient.SendAsync(stream, Convert.ToInt32(stream.Length), (async (completed) =>
        {
            if (completed)
            {
                clientResponse = await WSMessageSendSuccess(reqObject, callback);

                // Websocket response is flushed to the console window, but when it leaves the scope, it doesn't hold the response out of the SendAsync() scope.
                Console.WriteLine(clientResponse);
            }
            else
            {
                WSMessageSendFail(reqObject);
                clientResponse = "Failure to send Message";
            }
        })));


        while (response.Status != TaskStatus.RanToCompletion)
        {
            Task.Delay(10000).Wait();
        }
        response.Wait();

        // As soon as we leave scope of WebSocket.SendAsync() method, it clears the client response variable value.
        // variable name : clientResponse;, it also works same with static property/variable.
        return clientResponse;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SendAsync call, its OnMessage callback, and the Task.Run and polling code shown in the issue. Trace how WSMessageSendSuccess returns its response and how clientResponse is accessed after the callback completes. Done would require a documented or implemented request/response behavior that preserves the response beyond the SendAsync scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.