一、概述

本教程主要阐释了如何利用SignalR与消息队列的结合,实现不同客户端的交互

  • SignalR如何和消息队列交互(暂使用ActiveMQ消息队列)
  • SignalR寄宿在web中和其他SignalR、控制台客户端交互。
  • SignalR单独寄宿在控制台中和其他SignalR、控制台客户端交互。

下面屏幕截图展示了各个客户端通过ActiveMQ相互通信

  1、SignalR寄宿在web:

【转】SignalR与ActiveMQ结合构建实时通信

  2、SignalR寄宿在控制台中,web客户端调用SignalR,读者自行测试。

 工程目录:

【转】SignalR与ActiveMQ结合构建实时通信

 

一、创建项目

  1、创建生产者项目,该项目要是通过控制台输入消息,发送到消息队列

    创建控制台应用程序命名为ActiveMQNetProcucer,然后用包管理器安装ActiveMQ的.Net客户端

    Install-Package Apache.NMS.ActiveMQ

    主要代码如下:

 1 using Apache.NMS;
 2 using Apache.NMS.ActiveMQ;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 namespace ActiveMQNet
 9 {
10     class Program
11     {
12         static IConnectionFactory _factory = null;
13         static IConnection _connection = null;
14         static ITextMessage _message = null;
15 
16         static void Main(string[] args)
17         {
18             //创建工厂
19             _factory = new ConnectionFactory("tcp://127.0.0.1:61616/");
20 
21             try
22             {
23                 //创建连接
24                 using (_connection = _factory.CreateConnection())
25                 {
26                     //创建会话
27                     using (ISession session = _connection.CreateSession())
28                     {
29                         //创建一个主题
30                         IDestination destination = new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("topic");
31 
32                         //创建生产者
33                         IMessageProducer producer = session.CreateProducer(destination);
34 
35                         Console.WriteLine("Please enter any key to continue! ");
36                         Console.ReadKey();
37                         Console.WriteLine("Sending: ");
38 
39                         //创建一个文本消息
40                         _message = producer.CreateTextMessage("Hello AcitveMQ....");
41 
42                         //发送消息
43                         producer.Send(_message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
44                         while (true)
45                         {
46                             var msg = Console.ReadLine();
47                             _message = producer.CreateTextMessage(msg);
48                             producer.Send(_message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
49                         }
50                        
51                     }
52                 }
53 
54             }
55             catch (Exception ex)
56             {
57                 Console.WriteLine(ex.ToString());
58             }
59 
60             Console.ReadLine();
61 
62         }
63     }
64 }
View Code

相关文章:

  • 2022-02-12
  • 2022-01-19
  • 2021-04-12
  • 2021-08-22
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案