Wait for WebSocket response in netcore

Viewed 716

I have a desktop application which connects to a web server via WebSockets. While the connection is open the server can send the client application commands. When the client has executed the command he tells the server that he has completed the task. When this message arrives on the server the WebSocket middleware executes a ReceiveAsync method.

I need to await the response of the client on the server, since I need to do some additional stuff based on the initial command.

Here's and example on how a communication could be done. In this case we are going to tell the client to create a new file.

  1. Client connects to the server via Websocket. The server stores the Websocket with a GUID.
  2. The server sends a "CreateFile" command to the server.
  3. The client receives the command and create a new file.
  4. The client sends a "Created File with name abc.xyz" message back to the server.
  5. The server the registers the file in its internal database to do additional work.

My current state is an asynchronous SendMessage method that I can await. However now I need a way to await the response part. I already read about a possibility with the TaskCompletionSource and then Task.WhenAll(). I also read about the possibility with a ManualResetEvent.

Currently I'm a bit confused how I should approach this problem.

0 Answers
Related