SignalR追踪在线用户和聊天 [英] SignalR for tracking online users and chat

查看:2743
本文介绍了SignalR追踪在线用户和聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发社交网络应用程序,将实现几个新功能。
1.跟踪用户在线
2.聊天(一对一聊天,后来群聊)

We are working on a social network application and are going to implement couple of new features. 1. Tracking online users 2. Chat (one to one chat and later group chat)

我已经调查SingalR,似乎有希望的。我们正在使用ASP.NET MVC 3,并考虑使用集线器。
我的问题入手在于是否SignalR会更好,而不是简单的投票聊天?什么会更好至于可扩展性是什么呢?我看到的这样其他的问题,但没能找出其中的一个较好尽可能的可扩展性而言。

I have looked into SingalR and it seems promising. We are using ASP.NET MVC 3 and are thinking of using hubs. My question to start with is whether SignalR be better instead of simple polling for chat? What will be better as far as scalability is concerned? I have seen other questions on SO but was not able to find out which one of them is better as far as scalability is concerned.

第二个问题是,如果我们使用SignalR,我们可以用它来追踪也在线用户。我们可以定期调用间隔从每个客户端服务器端功能,说:我在网上,并在轮毂的方法,我们可以只设置isOnline位在DB。一旦客户端断开连接,我们可以不设置该位。将这项工作还是简单的投票是更好地在这里?我们如何设置用户离线如果我们用简单的投票?

The second question is if we use SignalR, can we use it to also track online users. We can call a server side function from each client at regular interval to say "I am online" and in the hub method we can just set the isOnline bit in the DB. Once the client is disconnected we can unset the bit. Will this work or is simple polling is better here ? How do we set user as offline if we use simple polling ?

感谢

推荐答案

我使用SignalR作为一种-聊天架构了。完美的作品我们唯一的IIS服务器设置。
对于可扩展性检查: Sclaing出-SignalR

I am using SignalR as a kind-of chat architecture too. Works perfectly an our single IIS server setup. For scalability check out: Sclaing-out-SignalR

如果您使用的集线器,您可以通过查询类似的这个

If you are using Hubs, you can solve the "I am online" problem by querying the connected clients like this

var clients = Hub.GetClients<Type of your hub here>();

和要求从每个客户端的用户名。如果有任何连接的损失,你必须找到在DB在线用户,是轮毂的没有更多的客户。

and request the UserID from each client. If there is a loss of any connection you have to find the in DB online users, that are no more clients of the hub.

另一种方法是在线设置用户从用户到轮毂的第一消息。 我是那里。
并使用该解决方案

Another approach is to set the user online as a first message from user to hub. "Hi I am there". And use this solution

public class MyHub : Hub, IDisconnect
{
    public Task Disconnect()
    {
        // Query the database to find the user by it's client id.
        var user = db.Users.Where(u => u.ConnectionId == Context.ConnectionId);
        return Clients.disconnected(user.Name);
    }
}

要处理断开事件。

希望我可以给你一些想法。

Hope I could give you some ideas.

这篇关于SignalR追踪在线用户和聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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