SignalR不是随时准备启动后()。()完成? [英] SignalR not always ready after start().done()?

查看:282
本文介绍了SignalR不是随时准备启动后()。()完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小项目,SignalR工作,但我得到了一些非常不一致的行为。

I have got a small project working with SignalR, however i am getting some very inconsistent behavior.

<script type="text/javascript">
    $(function () {
        var chat = $.connection.brewBattleHub;
        $.connection.hub.start().done(function () {
            $("#broadcast").click(function () {
                // Call the chat method on the server
                chat.server.roll($("#username").val(), $("#drinkname").val());
            });
            chat.server.sendMessage("SignalR loaded...");
        });
    });
</script>

当我加载网页,有时我看到SignalR加载的消息,其他时间我不是。

When i load the page, sometimes i am seeing the message "SignalR loaded", other times i am not.

还有就是在页面上一些其他的功能还,有时这也不管用。如果我点击按钮和使事情发生足够一气呵成它最终将全部来通过......从这个角度它是所有黄金和完美的作品。

There's is some other functionality on the page also, and sometimes this does not work either. If i click buttons and make things happen enough it will eventually all come through in one go... from this point it is all golden and works perfectly.

确实的start()。()完成?不能保证它是所有准备好了?

does start().done()? not ensure it is all ready?

===
编,我不是引用jQuery Mobile的(谷歌提到有这样做的时候错误)

=== addendum, i am not referencing jquery mobile (google mentioned there is a bug when doing so)

推荐答案

这周我也有类似的问题,只是在.done的code()绝然。我启用了日志记录,没有什么是永远记录下来。检查浏览器的控制台没有错误。展望在浏览器的开发工具,我可以看到,有没有网络活动时,我调用start()。我证实,$ .connection.myhubclass返回的代理有我就可以了所有的方法,所以我知道这可能跟我们的服务器和服务器端code的树立正确的。

I had a similar issue this week except that the code in .done() never ran. I enabled logging and nothing was ever logged. Checking the browser's console there were no errors. Looking in the browser's dev tools I could see that there was no network activity when I called start(). I confirmed that the proxy returned by $.connection.myhubclass had all my methods on it so I knew it could talk to the server and that the server side code was set up right.

一遍又一遍我确信,我没有做错什么检讨code和文件之后。 SignalR就是这么简单这是pretty很难这样做不对,如果你遵循的例子。我打我的书桌上头相当长的时间试图弄清楚这一点。

After reviewing the code and documentation over and over again I was convinced that I wasn't doing anything wrong. SignalR is so simple it's pretty tough to do it wrong if you follow the examples. I beat my head on the desk for quite a long time trying to figure this out.

然后我注意到,如果我从控制台运行SignalR的启动方法,它会工作。这使我相信,它试图太早开始连接。我解决了我的问题,从这个不断变化的初始code:

Then I noticed that if I ran SignalR's start method from the console it would work. That led me to believe that it was trying to start the connection too early. I solved my problems by changing the initialization code from this:

$.connection.hub.start().done(function () {
  //Do interesting stuff
});

要这样:

setTimeout(function () {
    $.connection.hub.start().done(function () {
      //Do interesting stuff
    });
}, 5000);

我敢肯定,我可以从5秒减少延迟的东西短,但额外的时间似乎是被需要的,让浏览器中得到自己准备创建的连接。一旦延误是有记录开始工作,连接上来后,服务器端OnConnected()跑,做()是在客户端上运行。

I'm sure I can reduce that delay from 5 seconds to something shorter but the extra time appeared to be what was needed to let the browser get itself ready to create the connection. Once that delay was in there the logging started working, the connection came up, the server side OnConnected() ran and done() was run on the client.

搞清楚了这一点后,我仔细检查了我的JavaScript加载顺序,想着也许我是装出来的东西秩序,但秩序是正确的根据SignalR样品。我加载了jQuery,SignalR,/ signalr /集线器,然后我的脚本初始化一切文件。

After figuring this out I double checked the order of my javascript loading, thinking maybe I was loading something out of order but the order is right according to the SignalR samples. I load jQuery, SignalR, /signalr/hubs, then my script file that initializes everything.

我,为什么需要该延迟开放的建议。我没有看到它的任何文档,所以我知道这可能是我做的事。幸运的是,此页面启动SignalR之前轻微延迟是没有问题的。

I am open to suggestions as to why that delay was needed. I don't see it in any of the docs so I know it could be something I did. Fortunately for this page a minor delay before starting up SignalR is not a problem.

这篇关于SignalR不是随时准备启动后()。()完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆