聊天框Demo:DotNetCore+ActiveMQ+Mqttjs 實現前後台消息監聽
網上查了查 ActiveMQ + .net core 的例子很少,自己做一個demo 作為記錄,另外FineUI Core基礎版要來了,出來後我會用FineUI再做一版,為知識星球的引流...
打開今日頭條,查看更多圖片1.安裝SDK 準備ActiveMQ 服務
此處用的 .net core 2.0+
此處用的 apache-activemq-5.9.0
2.創建項目
3.引用樣式
4.引用DLL
注意:先安裝 Apache.NMS.NetCore 再安裝 Apache.NMS.ActiveMQ.NetCore
5.抄襲一個ActiveMQHelp
來自
這裡未做任何修改,正常添加引用即可
6.寫代碼
引用
直接重寫 View/Home/Index.cshtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@{
ViewData["Title"] = "全球都比較大的聊天室";
}
@section body{
<h2>@ViewData["Title"]</h2>
<form id="form1"></form>
}
@section Scripts{
@*框架已經引用了*@
@*<script src="~/js/MQTT/paho-mqtt.js"></script>*@
<script>
$(document).ready(function () {
InitPage();//準備頁面
ItinPahoClient();//準備鏈接
});
//核心方法:通過MQtt連接
function ItinPahoClient() {
//地址 埠 鏈接名稱(不是主題)
var client = new Paho.Client("沒神的IP,大夥都知道", 61614, "jsclient");
//監聽事件
client.onMessageArrived = function (msg) {
//收到消息
messagebox.append("接收到消息:" + msg.payloadString);
$("#textbox").val("");
};
//鏈接配置
client.connect({
onSuccess: function () {
messagebox.append("鏈接成功");
//訂閱主題為myqueue
client.subscribe("myqueue");
//發布消息,主題為myqueue2
//client.publish("myqueue2", "Message from js client");
}
});
}
//發送消息
function setmessage() {
var v = $("#textbox").val();
bsEx.dopostback("@Url.Action("SetMessage")", {"msg":v}, function (t) {
eval("(" + t + ")");
});
}
///初始化頁面
function InitPage() {
//表單
new bsEx.ItemForm({
id: "form1",
renderto: "#form1",
items: [
{ type: "textarea", name: "messagebox", title: "消息", height: "300px" },
{ type: "text", name: "textbox", title: "發送" }
],
colunb: 1, showlabel: false, css: { "width": "500px" }
}).init().attr("onkeydown","if(event.keyCode == 13) { return false; }");
//發送按鈕
var btn = $("#textbox").addbutton("發送");
btn.on("click", function () {
setmessage();
})
$("#footer span").prepend("v" + $.fn.bsEx);
//重寫append 加個回車
messagebox = $("#messagebox");
messagebox.append = function () {
arguments[0] = arguments[0] +"
";
($.fn.append).apply(this,arguments);
}
messagebox.append("...鏈接中");
}
//再text後面加一個button
$.fn.addbutton = function (text) {
$(this).parent().addClass("input-group");
var bid = $(this).attr("id") + "-btn";
var b = $("<span class="input-group-addon btn" id="" + bid + "">" + text + "</span >");
$(this).after(b);
$(this).attr("onkeydown", "if(event.keyCode == 13) { setmessage(); }");
return $(b);
}
</script>
}
注意:前台的核心方法 ItinPahoClient();
修改 Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ActiveMQTest.Controllers;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace ActiveMQTest
{
public class Program
{
/// <summary>
/// MQ
/// </summary>
public static ActiveMQHelp mymq { get; set; }
public static void Main(string[] args)
{
//開始鏈接和訂閱主題
mymq = new ActiveMQHelp(isLocalMachine: false, remoteAddress: "沒神的IP,大夥都知道");<br> //主題名稱 myqueue 與前台一致 ,這裡注意區分 topic 和 queue的區別
Program.mymq.InitQueueOrTopic(topic: true, name: "myqueue", selector: false);
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
修改 Controllers/HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ActiveMQTest.Models;
namespace ActiveMQTest.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SetMessage(string msg)
{
//發送
Program.mymq.SendMessage(msg, "newid", priority: Apache.NMS.MsgPriority.Lowest);
return Content("console.log(""+ msg + " 發送成功");");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GetMessage()
{
//後台拿一次
return Content("console.log("" + Program.mymq.GetMessage() + "");") ;
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
至此,代碼全部完成,可以運行,只要IP對,基本沒啥大問題,不會的問我,
作者:
原文:https://www.cnblogs.com/shiworkyue/p/10424500.html
※HA+HDFS+HDFS-HA集群配置+YARN-HA配置+HDFS Federation架構設計
※C++ 單例模式總結與剖析
TAG:程序員小新人學習 |