助于 RabbitMQ 的 Web STOMP 插件,實現(xiàn)瀏覽器與服務(wù)端的全雙工通信嫂伞。從本質(zhì)上說,RabbitMQ 的 Web STOMP 插件也是利 WebSocket 對 STOMP 協(xié)議進行了一次橋接,從而實現(xiàn)瀏覽器與服務(wù)端的雙向通信猜煮。
插件安裝(默認不開啟)
我們進入 rabbitmq
的 sbin
目錄撼班,執(zhí)行下面的命令開啟stomp插件
rabbitmq‐plugins enable rabbitmq_web_stomp rabbitmq_web_stomp_examples
消息推送測試
在瀏覽器訪問http://localhost:15670 可以看到stomp的例子代碼 歧匈。
將stomp.min.js拷貝到 web工程,創(chuàng)建 websoket.html
<html>
<head>
<title>RabbitMQ Web STOMP Examples : Echo Server</title>
<meta charset="UTF‐8">
<script src="js/stomp.min.js"></script>
</head>
<script>
var client = Stomp.client('ws://localhost:15674/ws');
var on_connect = function(x) {
id = client.subscribe("/exchange/paynotify", function(d) {
alert(d.body);
});
};
var on_error = function() {
console.log('error');
};
client.connect('guest', 'guest', on_connect, on_error, '/');
</script>
</body>
</html>