0

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?

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • I don't think there's an easy way to do this with a SignalR hub, though there is a similar question on SO that outlines a way to do so with PersistentConnection: http://stackoverflow.com/questions/19885207/is-there-a-way-to-check-if-a-signalr-server-hub-is-active-up-ready-accepting-con (this uses jQuery though, so probably not viable if you have a C# backend). The inherent issue I see with this is that in order for the client to determine whether the server is available, it has to try connecting to it, which isn't going to happen if the server's offline anyway. – John Clifford Feb 09 '16 at 10:40
  • Saw the other thread as well but wasn't able to use it with a C# backend. – Mighty Badaboom Feb 09 '16 at 10:46

2 Answers2

1

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.

John Clifford
  • 821
  • 5
  • 10
  • Thought about this as well but I only want to connect to the server if the users clicked the button. Maybe I have to change this and connect automatically and add a Login-Method for the user click. – Mighty Badaboom Feb 09 '16 at 10:47
  • If you wanted to go that route what you could do is put the hub connection code into the click method for the button, but then you still don't really have a way to make sure the server is available before trying to connect to it. Sorry I couldn't be more help than this. – John Clifford Feb 09 '16 at 10:58
  • You've been a great help beause I had an idea whoch I wouldn't had without your hint ;) Like I wrote in the last comment. I will connect to the server on startup and will give the server a login method where the client will "log in" when the user pressed the button. And if the server isn't available I will see this :) – Mighty Badaboom Feb 09 '16 at 11:40
0

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.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51