SignalR and Tabs , windows , cloned frames?

妖精的绣舞 提交于 2019-12-13 07:06:19

问题


I created a simple app which accept a text message from a client and reply to that specific connection : "Hey I got your message" , and besides that , broadcast the message to all others.

Piece of code :

 1     protected override Task OnConnected(IRequest request, string connectionId)
 2       {
 3                return Connection.Send(connectionId, "Welcome!");
 4       }
 5        
 6     protected override Task OnReceived(IRequest request, string connectionId, string data)
 7       {
 8         Connection.Send(connectionId, "Connection " + connectionId + " I got your message " + data);
 9         return Connection.Broadcast(data);
 10      }

All is OK.

Question #1 Does Connection ID is 100% unique to the initializer ? what about if I clone a browser tab ?, or cloned Iframe ? from my testing it is unique. but I need to make sure.

Question #2

looking at line #8 , I couldn't write return Connection.Send because the other line won't be executed . however I think , that way , I will lose the TASK<> return value . what if i'll need it ?

OnReceived returns a Task but currently it returns just the Connection.Broadcast(data);'s task and (as I was saying - I'm losing line's #8 task return object.)Im afraid im doing something wrong here. or maybe I'm not ?

Who uses this task result in the app cycle anyway?


回答1:


1: Yes connection ID is always unique
2 The Connection.Send will not return a value that is returned via the client, SignalR does not support that. However, if you're interested in just waiting for it and getting the status of the task you can do .Wait() on the Connection.Send() or you can ContinueWith,

Aka

Connection.Send(...).Wait(); // If there's an error this will throw
return Connection.Broadcast(data);

OR

  Connection.Send(....).ContinueWith(task => {
        if(!task.IsFaulted) 
        {
            // Task ran successfully    
        }
        else 
        {
            // Something went wrong when sending, you can get the exception from the task
        }
    });
    return Connection.Broadcast(data);


来源:https://stackoverflow.com/questions/16604848/signalr-and-tabs-windows-cloned-frames

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!