Announcement

Collapse
No announcement yet.

Async wait for signalR event

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

  • ochensati
    started a topic Async wait for signalR event

    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

  • ochensati
    replied
    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.

    Leave a comment:


  • Vladimir
    replied
    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.

    Leave a comment:


  • ochensati
    replied
    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.

    Leave a comment:


  • geoffrey.mcgill
    replied
    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.

    Leave a comment:


  • ochensati
    replied
    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.

    Leave a comment:


  • ochensati
    replied
    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?

    Leave a comment:


  • geoffrey.mcgill
    replied
    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);
    })()
    */

    Leave a comment:


  • geoffrey.mcgill
    replied
    Hi. Maybe the SignalR Retyped package could help here?

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

    install-package retyped.signalr

    Leave a comment:


  • ochensati
    replied
    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);
    })()

    Leave a comment:

Working...
X