SignalR: Hub OnConnected not called if no client methods

后端 未结 2 1695
轮回少年
轮回少年 2021-02-12 23:57

I am writing a game in SignalR. The idea is that you connect with your laptop, which will be used as \"display\" and then you connect with your smart phone and that will be used

2条回答
  •  清歌不尽
    2021-02-13 00:43

    A way around this is to just have a fake Hub that you wire up to. Does nothing, returns void. At least this way, when you bootstrap your game, before you call .start() on the connection, you wire up to this fake method on the fake hub. This will get you the onConnected() method.

    I agree, this is such odd behaviour. If you make a connection, onConnected() should be raised, even if you're not listening to events.

    What makes this even more odd is if you're doing Hub authorization. If you derive from the AuthorizeAttribute, in your case, the AuthorizeHubMethodInvokation() will be called for each method you call on the client, but the AuthorizeHubConnection() will never be called.

    --Update

    I played around with this on my project. I had the iniital idea of creating a hub proxy on the client that doesn't exist on the server, but this fails. However, you can just hook up to a real hub but a fake callback. This does work and lets you get away with creating a real method.

    This is clearly a horrible work around, but in the face of nothing else, does the job.

提交回复
热议问题