简体   繁体   English

从何处开始Signalr和Webforms聊天应用程序的聊天集线器连接

[英]Where to start chat hub connection for Signalr and Webforms chat application

Im am writing a one-to-one chat in Signalr in a legacy Webforms project (note VB.net). 我正在使用旧版Webforms项目(请注意VB.net)在Signalr中进行一对一聊天。 Basically Im looking for a result like Facebook chat, so that basically as soon as a user logs in, they are classed as 'online'. 基本上,我正在寻找类似Facebook聊天的结果,因此基本上,只要用户登录,他们就被归类为“在线”。

I have some success. 我有一些成功。 In my login method, I am adding my logged in user to a static list of OnlineUsers. 在我的登录方法中,我将已登录的用户添加到OnlineUsers的静态列表中。 I have a Chat.aspx page which when I refresh, starts the SignalR hub connection and I can see the logged in User (loaded from OnConnected method). 我有一个Chat.aspx页面,当我刷新该页面时,它将启动SignalR集线器连接,并且我可以看到登录的用户(从OnConnected方法加载)。

Obviously I dont want to have to reload the Chat.aspx page to see any users that have just logged in, so my question is, is it safe/efficient to start the connection from say my Masterpage so that all the UI is updated when a user comes on/offline? 显然,我不想重新加载Chat.aspx页以查看刚刚登录的任何用户,所以我的问题是,从说我的母版页开始连接是否安全/高效,这样当用户上线/下线? Im guessing this will cause loads of unneccessary connections? 我猜这会导致不必要的连接负载吗?

Here is my code. 这是我的代码。

Login success (adds to a static list of OnlineMembers 登录成功(添加到OnlineMembers的静态列表中)

OnlineMember.AddOnlineMember(member.UserId, member.UserName)

Chat.aspx Chat.aspx

$.connection.hub.start(function () { chat.server.getAllOnlineStatus(); });

Raj, I think you would be best off to call a routine the moment someone is actually logging in. Raj,我认为您最好在有人真正登录时立即调用例程。

If you have a login routine that adds the users to OnlineUsers list then you can trigger an update of ALL clients by calling a method that updates the list of online users. 如果您具有将用户添加到OnlineUsers列表的登录例程,则可以通过调用更新在线用户列表的方法来触发所有客户端的更新。

A function inside your Chat.aspx page that runs at any time other than login will generate load needlessly. Chat.aspx页内的某个函数(除登录外)可在任何时间运行,将不必要地产生负载。 The beauty of SignalR is the persistence to the client(s) and how you can target "All" or "Caller" when triggering client-side routines. SignalR的优点是对客户端的持久性以及触发客户端例程时如何定位“全部”或“调用者”。

(Here is a server-side hub call to a routine registered in the ASPX. You could pass data to it as an argument if required.) (这是对在ASPX中注册的例程的服务器端集线器调用。如果需要,可以将数据作为参数传递给它。)

The following could be done after successful login of a new client: ie Clients.Caller.updateOnlineUserList(); 成功登录新客户端后,可以执行以下操作:即Clients.Caller.updateOnlineUserList(); -or- Clients.Caller.updateOnlineUserList(someData); -或-Clients.Caller.updateOnlineUserList(someData);

(Obviously you'd need to write the code for "updateOnlineUserList" which would update the user list displayed on the page.) (显然,您需要为“ updateOnlineUserList”编写代码,以更新页面上显示的用户列表。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM