简体   繁体   中英

Connecting to a SignalR server

On the server I create a Hub

public class SGHub : Hub
{
    public static List<string> Users = new List<string>();

    public void Send(string name, string message)
    {
        Clients.All.broadcastMessage(name, message);
        Console.WriteLine(SGHub.Users.Count);
    }
}

On the client I connect to the Hub

void Start()
{
    hubConnection = new HubConnection(serverURL);
    hubConnection.Error += HubConnectionError;
    iHubProxy = hubConnection.CreateProxy("SGHub");
    Subscription subscription = iHubProxy.Subscribe("broadcastMessage");
    hubConnection.Start();
}

If the server is not running, and the client tries to connect to the Hub, the application hangs, how to avoid this?

You can try to start flow with an http request to serverURL

and only if server returns , you start signalr connection flow. ,您才启动信号器连接流程。

I would suggest to put you clientside connection code in a thread. That's how I did it. Every time you have a long running task (like waiting for a timeout in your case) and you do this in the UI thread the app will freeze.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM