简体   繁体   中英

SignalR: Check if server is available

I have a C# server and a C# client using SignalR to communicate. Both are WPF applications. On my client I have a button for logging in at the server. I want to disable the login button if the server is not available.

Is there any way I can check if the server is available or not?

You could override the hub's OnConnected method and have it call a method on the client which enables the button, then override OnDisconnected and have it call a method which disables the button. Although this isn't "checking the server is online" per se, it means you'll only be able to log in if the client managed to connect to the hub.

I solved the problem using a thread where I'm trying to connect to the server in an endlees loop. If I get an exception I know the server is not available and after some seconds I try it again. I'm raising events when the connection to the server was possible or not. Works like a charm since one year.

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