SignalR - 建立连接以相当长的时间 [英] SignalR - Establishing Connection Taking quite a long time
问题描述
我刚开始与SignalR工作的实时更新。我需要在建立连接之后执行一些操作。所以我已经写在.done code()方法。
I have just started working with SignalR for realtime update. I need to perform some operation after the connection is established. so I have written that code in .done() method.
我现在面临的问题是:建立连接时需要一些时间,因为,我不能够执行它,我打算操作。继我的JS code片段。
The issue I am facing is : establishing connection takes some time and due to that I am not able to perform the operation which I am intended to. Following code snippet of my js.
$(function () {
// Proxy created on the fly
projectHub = $.connection.projectHub;
$.connection.hub.logging = true;
// Start the connection
$.connection.hub.start().done(function () {
$("#lnkFollowProject").live("click", function () {
console.log("Follow click");
projectHub.server.followProject(projectId, currentLoggedInUserId);
});
$(".lnkUnfollowProject").live("click", function () {
console.log("Unfollow click");
projectHub.server.unfollowProject(projectId, currentLoggedInUserId);
}); }); });
我不知道为什么它是需要时间来建立连接。当我试着用简单的聊天应用程序,它工作得很好。我做了该网页许多其他功能,还利用基因敲除绑定。 (不用于上述功能)。我还搜查,发现这个杀毒即可受害者。我尝试禁用了太多,但没有收获。
I am not sure why it is taking time to establish connection. As i tried with simple chat application and it works well. I am doing many other functionality for that page and also using knockout for binding. (not for the above functionality). I have also searched this and found antivirus can be victim. I tried disabling that too but no gain.
我知道有一个解决方案,直到连接建立和上完成启用它,我可以禁用按钮。但我不希望用户限制。
I know one solution that I can disable button til connection is establish and on done enable it. But i don't want user to restrict.
请更新我,如果任何其他可能的问题。
Please update me if any other probable issues.
先谢谢了。
推荐答案
我对code性能的两个建议。
I have two suggestions about the code performance.
1)使用。对()方法,而不是.live()
1) Use .on() method instead of .live()
使用.live()方法,因为jQuery的报价.delegate和方法。对的后续版本不再推荐。
出现与使用.live()的下列问题:
Using the .live() method is no longer recommended since the later versions of jQuery offer .delegate and .on methods. the following issues arise with the use of .live():
Ⅰ)的jQuery试图调用.live()方法,这可能是耗时的大型文档之前检索由选择器指定的元素
I) jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.
二)由于所有.live()事件在文档元素连接,它们的事件进行处理之前,时间最长和最慢的可能路径。
II) Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.
2)取单击事件出来.done()方法,因为followProject和unfollowProject的方法都需要先点击事件虎自己。
2) Take click events out of .done() method, because "followProject" and "unfollowProject" methods both need click event to tiger themselves first.
见下文code:
$(function () {
// Proxy created on the fly
projectHub = $.connection.projectHub;
$.connection.hub.logging = true;
// Start the connection
$.connection.hub.start();
$(document.body).on("click","#lnkFollowProject", function () {
console.log("Follow click");
projectHub.server.followProject(projectId, currentLoggedInUserId);
});
$(document.body).on("click",".lnkUnfollowProject", function () {
console.log("Unfollow click");
projectHub.server.unfollowProject(projectId, currentLoggedInUserId);
});
});
这篇关于SignalR - 建立连接以相当长的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!