在Hub类中等待来自RabbitMQ的消息

时间:2017-05-24 19:27:18

标签: c# .net asp.net-mvc rabbitmq signalr

我使用ASP.NET MVC + SignalR将消息从服​​务器发送到客户端。

public class ChatHub : Hub
{
    static List<User> Users = new List<User>();

    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }

    public void Connect(string userName)
    {
        var id = Context.ConnectionId;
        if (!Users.Any(x => x.ConnectionId == id))
        {
            Users.Add(new User { ConnectionId = id, Name = userName }); 
        }
    }

    public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {
        var item = Users.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
        if (item != null)
        {
            Users.Remove(item);
        }

        return base.OnDisconnected(stopCalled);
    }
}

如何等待来自RabbitMQ的消息将这些消息发送到ASP.NET MVC的客户端?

例如,我可以在Console Application中编写调用“ReadLine()”方法并等待来自RabbitMQ的消息:

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{

};
channel.BasicConsume(queue: queueName,
                     noAck: true,
                     consumer: consumer);
Console.ReadLine();

0 个答案:

没有答案