Announcement

Collapse
No announcement yet.

Async wait for signalR event

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Async wait for signalR event

    Hello,
    I need to make a request to the webserver through signalR, then wait for the response
      public async Task<object> Ask(object msg)
            {
                //create the messenger
                var sender = new AskActor("asktemp");
                Sender = sender;
                var noMessage = sender.message;
                //send the message
                RecieveTarget(msg);
    
                //wait for signalR to send back result
                while (sender.message == noMessage)
                {
                    //turn over control for a while
                    await Task.Delay(100);
                }
                //kill the messenger
                sender.Kill();
                //return message
                return sender.message;
            }
    SignalR does not provide a promise or any other form of syncronization, so I just need to put this task on park, allow the java event loop to move to handle the signalR return, and then check if anything happened.

    Is this possible?

    Thank you

    #2
    This is basically what I am trying to achieve, but the async in bridge does not seem to transfer to the javascript

     (async () => {  
     await send_semail();  
     const { url } = await new Promise(resolve => maildev.on('new', resolve));
      await visit(url);
    })()

    Comment


      #3
      Hi. Maybe the SignalR Retyped package could help here?

      https://retyped.com/?q=signalr#search

      install-package retyped.signalr

      Comment


        #4
        You can also add raw JavaScript within the C# by using the following comment block style:
        /*@
        (async () => {  
          await send_semail();  
          const { url } = await new Promise(resolve => maildev.on('new', resolve));
          await visit(url);
        })()
        */

        Comment


          #5
          SignalR is working just fine. The specific thing that I need to achieve is a wait for the signalR response. Is there a way to Bridge.net await the javascript async function?

          Comment


            #6
            I solved the signalR problem with IPromise:

             public async Task<object> Ask(object msg)
                    {
                        var asker = new AskActor("asktemp");
                        var oSender = Sender;
                        Sender = asker;
                        var result = await AskPromise.Get(msg, asker, ReceiveTarget); //Ipromise watches signalR for correct response
            
                        Sender = oSender;
                        //return message
                        return msg;
                    }
            The next question is it possible to get the next procedure to be synchronous i.e.
            var ask =Ask(question);
            Task.WaitAll(ask);
            var answer = ask.Result;
            it seems WaitAll or any Wait is not available.

            Comment


              #7
              Some .Wait support is available, see https://github.com/bridgedotnet/Bridge/issues/2406

              We are investigating options to enhance .Wait and add support for .WaitAll.

              Comment


                #8
                Ok, I downloaded the latest version, but it is still not working like C#:
                This is my code
                  public object Ask(object msg)
                        {
                            var w = AskAsync(msg);
                            w.Wait();
                            return w.Result;
                        }
                This is what is transpiled:
                 public  async object Ask(object msg)
                        {
                            var w = AskAsync(msg);
                            await w.WaitTask();
                            return w.Result;
                        }
                This is worse than not having the Wait, as the whole thing does not report to the calling procedures that it is async at compile. Wait should act like Thread.Join.

                Comment


                  #9
                  It cannot work like in C# because Javascript environment is a single thread. Thread.Join blocks calling thread but in a browser any block (like loop which check task status) will hang the browser until a function with Wait returns the control. So, full mimic of Wait is not possible IMHO.

                  Comment


                    #10
                    That makes sense. Is it possible to specify that you want the transpiled code to use more the javascript async/await pattern? Debugging through the outputted switches is a terror within the browser.

                    Comment

                    Working...
                    X