Use Signalr to have a facebook like notification s

2019-03-09 13:16发布

I want to implement a facebook like notification system in ASP.NET MVC 3 : notifications are sent to a specific user to notify him for an action on one of his items.

Is signalr suited for such requirement? How could i send a notification to a specific user (all opened sessions of this user) using SignalR?

Edit

Ok, Here what i did

In the client side

$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
var username = '@Html.ViewContext.HttpContext.User.Identity.Name';
// Declare a function on the chat hub so the server can invoke it
chat.addMessage = function (message) {
     $('#messages').append('<li>' + message + '</li>');
};
// Start the connection
$.connection.hub.start(function (){
    chat.join(username);
});

});

In the server side

public class Chat : Hub
{
    public void Join(string username)
    {
        AddToGroup(username);
    }
}

And every time i need to notify a user in the controller i do the following:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<Chat>();
clients[username].addMessage("test");

2条回答
我只想做你的唯一
2楼-- · 2019-03-09 13:53

It is appropriate for this or you use polling, those are the two choices.

Heres a brand new video from today on this:

http://channel9.msdn.com/Shows/Web+Camps+TV/Damian-Edwards-and-David-Fowler-Demonstrate-SignalR?utm_source=dlvr.it&utm_medium=twitter

查看更多
疯言疯语
3楼-- · 2019-03-09 14:00

Yes, SignalR is a good choice for that. Take a look at the documentation regarding Hubs (server and JS client).

You need to implement the server logic to associate your client's session with SignalR's session. You can use groups to notify all the open sessions of each user.

查看更多
登录 后发表回答