SignalR Hubを使ったときOnDisconnected、OnConnectedが動かない

ちょっとしたメモ書き

Hubを使ってサーバーサイド、クライアントサイドと実装をしていったのですが、どうしてもサーバーサイドの、接続状態を検出する「OnConnected」「OnDisconnected」は呼び出されず。

調べてみたところ・・・(実際には見つけてもらったもの!

ASP.NET SignalR Hubs API Guide – JavaScript Client

Note: Normally you register event handlers before calling the start method to establish the connection. If you want to register some event handlers after establishing the connection, you can do that, but you must register at least one of your event handler(s) before calling the start method. One reason for this is that there can be many Hubs in an application, but you wouldn’t want to trigger the OnConnected event on every Hub if you are only going to use to one of them. When the connection is established, the presence of a client method on a Hub’s proxy is what tells SignalR to trigger the OnConnected event. If you don’t register any event handlers before calling the start method, you will be able to invoke methods on the Hub, but the Hub’s OnConnected method won’t be called and no client methods will be invoked from the server.

で、要約してみると
通常、クライアント側でstartメソッドを呼び出す前に、クライアント側でイベントハンドラを登録して使いますと(onメソッドでサーバーからの呼び出しに備える)

それをしない場合、「OnConnected」は呼び出されないぞ!と
で、どうしても後から追加するようなものがある場合は、最低限一つは(onメソッドで)ハンドラを追加しておく必要がありますと。

なので、通常使い方としては、Proxyなど設定、ハンドラの登録をした後にクライアント側のstartメソッドを呼び出すということのようですね。

で、そのようにしたところ無事に動く!というところで落ち着きました。

ただこれだけでは、OnConnectedはうまくいっても、OnDisconnectedは不確実な呼び出しになりますので、私が使いたかった用途にはもう少し工夫が必要でした。

後日!