简体   繁体   English

在树莓派中向 RabbitMq 的网页之间发送消息

[英]send a message between a webpage to RabbitMq in raspberry pi

I'm trying to send a message between a webpage to RabbitMq in raspberry pi.我正在尝试在网页之间向树莓派中的 RabbitMq 发送消息。 The send.js and receive.js from the RabbitMq tutorial works fine. RabbitMq 教程中的 send.js 和 receive.js 工作正常。 However, when adding a link to the javascript file in the HTML is doesn't work但是,当在 HTML 中添加指向 javascript 文件的链接时不起作用

<HTML>  
<head>  
<script type = "text/javascript" src="send.js"></script>  
</head>  
<body>  
<p>Click the following button to see the function in action</p>  
<input type = "button" onclick = "myfunction()" value = "Display">  
</body>  
</html>  

Thesend.js is the one in the website's tutorial. send.js是网站教程中的一个。 The only change I made is adding the function heading我所做的唯一更改是添加 function 标题

function myfunction () {


  var amqp = require('amqplib/callback_api');

  amqp.connect('amqp://localhost', function(error0, connection) {
    if (error0) {
       throw error0; 
         ....
            .....
              ....

IS there anything that I'm missing?有什么我想念的吗?

require is a feature of CommonJS modules. require是 CommonJS 模块的一个特性。 This is the default module system used by Node.js and is not supported by web browsers.这是 Node.js 使用的默认模块系统,web 浏览器不支持。

While some modules can be converted for use in browsers using a bundler such as Webpack or Parcel, the one you are using is clearly marked as being for Node.js and almost certainly depends on APIs that are available in Node.js but not in browsers.虽然可以使用捆绑器(例如 Webpack 或 Parcel)将某些模块转换为在浏览器中使用,但您正在使用的模块明确标记为用于 Node.js 并且几乎可以肯定依赖于 Z3B2819DD2C24EDA251FAF25 中可用但浏览器中不可用的 API。

Your code will not run in a browser.您的代码不会在浏览器中运行。

You could write a web service using Node.js (eg using the Express.js module) which uses amqplib and then access it using the fetch API which is natively supported in browsers.您可以使用使用 amqplib 的 Node.js(例如使用 Express.js 模块)编写 web 服务,然后使用浏览器原生支持的获取 API访问它。 Alternatively, if you need the server to be able to initiate a message, useWebsockets .或者,如果您需要服务器能够发起消息,请使用Websockets

The idea of having a live connection via websites to a RabbitMQ service goes around WebSockets.通过网站与 RabbitMQ 服务建立实时连接的想法围绕着 WebSockets。 To this approach we need a plugin installed on the RabbitMQ service, then on the client side, opening a WebSocket to connect with the plugin.对于这种方法,我们需要在 RabbitMQ 服务上安装一个插件,然后在客户端,打开一个 WebSocket 来连接插件。 The plugin is a kind of bridge between WS and the broker.该插件是 WS 和代理之间的一种桥梁。

According to my experience, Paho.js and STOMP are two existing solutions.根据我的经验,Paho.js 和 STOMP 是两个现有的解决方案。 Paho.js is limited to connecting to topics only, while STOMP is a complete utilitarian tool. Paho.js 仅限于连接到主题,而 STOMP 是一个完整的实用工具。

To enable the STOMP plugin on your RabbitMQ server:要在 RabbitMQ 服务器上启用 STOMP 插件:

rabbitmq-plugins enable rabbitmq_stomp

Then install the bridge too:然后也安装网桥:

rabbitmq-plugins enable rabbitmq_web_stomp

Now your server is ready to be used.现在您的服务器已经可以使用了。 Let's jump into the client side.让我们进入客户端。 Download this file and put it somewhere in your project.下载此文件并将其放在项目中的某个位置。 Now the socket connection part:现在是socket连接部分:

import { Stomp } from './stomp';

var ws = new WebSocket('ws://yourServerAddress:15674/ws');
var client = Stomp.over(ws);

var on_receive =  function(data) {
    console.log('received:', data.body);
};
var on_connect = function() {
    console.log('connected');
    client.subscribe('/amq/queue/your-queue', on_receive)
};
var on_error =  function() {
    console.log('error');
};

client.onreceive = on_receive;
client.connect('yourUsename', 'thePasswd', on_connect, on_error, '/');

function send(payload, headers={}){
  client.send('/amq/queue/your-queue', headers, JSON.stringify(payload));
}
  • Keep in mind I wrote this code to connect straight to a queue.请记住,我编写此代码是为了直接连接到队列。 For topics just change the subscription endpoint to /amq/topic/your-topic对于主题,只需将订阅端点更改为/amq/topic/your-topic

You can see the full documentation here .您可以在此处查看完整的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM