javascript 中使用 ActiveMQ

news2025/2/23 5:44:15

javascript 中使用 ActiveMQ

1. 参考文档

https://www.eclipse.org/paho/files/jsdoc/index.html

2. html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="connect-input-box">
        <label for="host">host:</label>
        <input type="text" name="host" placeholder="input host" value="127.0.0.1"><br>
        <label for="port">port:</label>
        <input type="text" name="port" placeholder="input port" value="61614"><br>
        <label for="clientId">client id:</label>
        <input type="text" name="clientId" placeholder="input client id"><br>
        <label for="userId">user id:</label>
        <input type="text" name="userId" placeholder="input user id" value="user"><br>
        <label for="password">password:</label>
        <input type="text" name="password" placeholder="input password" value="pass"><br>
        <label for="destination">destination:</label>
        <input type="text" name="destination" placeholder="input destination" value="world"><br>
        <button id="connect" type="submit">connect</button>
        <button id="disconnect" type="submit">disconnect</button>
    </div>
    <div class="log-box">
        <p id="log-show"></p>
    </div>
    <div class="send-message-box">
        <label for="topic">topic:</label>
        <input type="text" name="topic"><br>
        <label for="queue">queue:</label>
        <input type="text" name="queue"><br>
        <input type="text" name="message"><br>
        <button id="send">send</button>
    </div>
    <div class="subscribe-box">
        <label for="subscribe-topic">subscribe-topic:</label>
        <input type="text" name="subscribe-topic">
        <button id="subscribe">subscribe</button>
    </div>
    <div class="unsubscribe-box">
        <label for="unsubscribe-topic"></label>
        <input type="text" name="unsubscribe-topic">
        <button id="unsubscribe">unsubscribe</button>
    </div>

    <script src="plugins/jquery-1.10.1.js"></script>
    <script src="plugins/mqttws31.js"></script>
    <script>
        $(() => {
            $('input[name="clientId"]').val("example-" + Math.floor(Math.random() * 10000))

            if (!window.WebSocket) {
                console.log('不支持WebSocket')
            } else {
                
            }
        })

        var client, destination

        $('#connect').click(() => {
            var host = $('input[name="host"]').val()
            var port = $('input[name="port"]').val()
            var clientId = $('input[name="clientId"]').val()
            var user = $('input[name="userId"]').val()
            var password = $('input[name="password"]').val()

            destination = $('input[name="destination"]').val()

            // 创建一个client 实例
            client = new Paho.MQTT.Client(host, Number(port), clientId)
            console.log(client)

            // 设置回调函数
            client.onConnectionLost = onConnectionLost
            client.onMessageArrived = onMessageArrived

            // 连接client
            client.connect({onSuccess: onConnect})
        })

        // 当client连接时调用
        function onConnect() {
            console.log('onConnect')
            client.subscribe(destination)
            message = new Paho.MQTT.Message("Hello")
            message.destinationName = destination
            client.send(message)
        }

        // 当客户端断开连接时被调用
        function onConnectionLost(responseObject) {
            if (responseObject.errorCode !== 0) {
                console.log("onConnectionLost: " + responseObject.errorMessage)
            }
        }

        // 当消息发送时调用
        function onMessageDelivered(message) {
            console.log('onMessageDelivered: ' + message.payloadString)
        }

        // 当消息到达时调用
        function onMessageArrived(message) {
            console.log("onMessageArrived:" + message.payloadString);
        }

        // 断开连接
        $('#disconnect').click(() => {
            console.log('disconnect');
            client.disconnect()
        })

        // 发送消息
        $('#send').click(() => {
            console.log('send')
            let topic = $('input[name="topic"]').val()
            let payload = $('input[name="message"]').val()
            let message = new Paho.MQTT.Message(payload)
            message.destinationName = topic
            client.send(message)
        })

        // 订阅主题
        $('#subscribe').click(() => {
            console.log('subscribe')
            let filter = $('input[name="subscribe-topic"]').val()
            let subscribeOptions = {
                qos: 2,
                invocationContext: {},
                onSuccess: function(val) { console.log(val) },
                onFailure: function(val) { console.log(val) },
                timeout: 60
            }
            client.subscribe(filter, subscribeOptions)
        })

        // 取消订阅主题
        $('#unsubscribe').click(() => {
            console.log('unsubscribe')
            let filter = $('input[name="unsubscribe-topic"]').val()
            let subscribeOptions = {
                invocationContext: {},
                onSuccess: function(val) { console.log(val) },
                onFailure: function(val) { console.log(val) },
                timeout: 60
            }
            client.unsubscribe(filter, subscribeOptions)
        })
    </script>
</body>
</html>

3. 测试

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/428071.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot——短信发送、手机验证码登录

目录 一、短信发送 1.1 阿里云短信服务 1.1.1 设置短信签名 1.1.2 模板管理 1.1.3 设置AccessKey 1.2 短信发送——代码开发 1.2.1 导入maven坐标 1.2.2 调用API 1.2 手机验证码登录 1.2.1 用户数据库表 1.2.2 修改过滤器 1.2.3 随机生成验证码的工具类 1.2.4 手机验证码登…

【Linux系统查找错误单词项目和vim的三种模式(命令,输入和底线命令模式)和项目的心得体会】

查找文章中拼写错误的单词的C语言程序 编写的C语言程序&#xff1a; #include<stdio.h> #include<string.h> int main() { FILE *fpfopen("essay.txt","r"); FILE *fp1fopen("words.txt","r"); if(fpNULL&&fp1NU…

CorelDRAW 2022矢量图形制作排版软件图文安装教程

CorelDRAW 2022是一款矢量图形制作和排版软件&#xff0c;做为目前图形设计类软件的领航者&#xff0c;CorelDRAW不断在每个新版本中完善功能&#xff0c;突破自我&#xff0c;并且广泛应用于印刷、出版、平面设计制作、包装设计&#xff0c;工业设计&#xff0c;以及服装设计裁…

学会这10款AI绘画工具,你也可以成为插画师

有些人担心人工智能绘画工具会取代插画师&#xff0c;但与其害怕&#xff0c;不如拥抱新技术&#xff01; 跟随本文了解和掌握新技术。如果你是插画师&#xff0c;以下五个人工智能绘画网站是必要的&#xff1a;「即时AI」&#xff0c;Lexica&#xff0c;DALLLeonardo和Scribb…

C++中文件操作与文件流

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C &#x1f525;座右铭&#xff1a;“不要等到什么都没有了&#xff0c;才下…

Flink SQL 在美团实时数仓中的增强与实践

01 Flink SQL 在美团 目前 Flink SQL 在美团已有 100业务方接入使用&#xff0c;SQL 作业数也已达到了 5000&#xff0c;在整个 Flink 作业中占比 35%&#xff0c;同比增速达到了 115%。 SQL 作业的快速增长给我们带来了许多新的问题和挑战&#xff0c;主要包括以下几点&#…

CTR-GCN 论文解读

论文名称&#xff1a;Channel-wise Topology Refinement Graph Convolution for Skeleton-Based Action Recognition论文下载&#xff1a;https://arxiv.org/pdf/2107.12213.pdf论文代码&#xff1a;https://github.com/Uason-Chen/CTR-GCN CTR-GCN 是中科院自动化所发表在 IC…

项目二,easyx的加入

今天是项目的第二天 与昨天的迷离比起来今天大大的有进步 我对实现啥的功能有了一个基本的方案 1对所有路的查询那就是用dfs 2对最短路的查询就是用的floyed但是,查询是要保存路径的,所以想到了,保存每条最短路的时候 同时把最短路的前驱保存下来,我们可以通过前驱把完整的…

InnoDB中索引的实现

B树索引的使用 5.6.1 不同应用中B树索引的使用 在OLTP应用中&#xff0c;查询操作只从数据库中取得一小部分数据&#xff0c;一般可能都在10条记录以下&#xff0c;甚至在很多时候只取1条记录&#xff0c;如根据主键值来取得用户信息&#xff0c;根据订单号取得订单的详细信息…

Linux下最小化安装CentOS-7.6(保姆级)

文章目录安装包开始安装一、 新建一个虚拟机二、配置安装CentOS7.6二、开始安装CentOS三、配置CentOS并下载基本信息安装包 链接&#xff1a;https://pan.baidu.com/s/1DodB-kDy1yiNQ7B5IxwYyg 提取码&#xff1a;p19i 开始安装 一、 新建一个虚拟机 1、 打开VMWare&#x…

UVCCamera 源码解析

本文将通过UVCCamera源码研究 概述相机传感器数据采集至USB数据传输整个过程 下载源码配置调试环境 git clone GitHub - saki4510t/UVCCamera: library and sample to access to UVC web camera on non-rooted Android device 注释调原有 ndk-build编译方式 //tasks.with…

使用 /permissive- 编译器开关时变量名or被识别为关键字

使用 /permissive- 编译器开关时变量名被识别为关键字 VS2022已经默认开启对c14的支持。并且&#xff0c;一般同时开启的还有/permissive-开关。项目属性如下所示&#xff1a; 然而&#xff0c;在许多早期代码中&#xff0c;函数名和变量名使用了c14等新标准中的关键字&#…

德国访问学者申请签证事宜

德国访问学者长期签证所需材料,下面就随知识人网小编一起看一看。 1、护照及复印件两份。 2、申请表附加表两份(里面有好多注意事项&#xff0c;最重要的是用德文填写&#xff0c;当时我就谷歌翻译后写上去的&#xff0c;最后附加表上的地点&#xff0c;有好多人问填办大使馆还…

谷歌seo快速排名优化方法,谷歌seo需要哪些技术手段?

谷歌seo快速排名优化方法怎么做&#xff1f; 答案是&#xff1a;利用GLB推广技术可以实现谷歌快速排名。 具体请阅读&#xff1a;什么叫GLB外推&#xff1f; 你只想快速曝光你的企业品牌&#xff0c;GLB外推技术是一个不错的选择。 但如果你想优化自己的外贸官网&#xff0…

【springcloud】服务熔断——Hystrix

官方资料&#xff1a;Home Netflix/Hystrix Wiki GitHub 服务雪崩 多个微服务之间调用的时候&#xff0c;假设微服务A调用微服务B和微服务C&#xff0c;微服务B和微服务C又调用其他的微服务&#xff0c;这就是所谓的“扇出”&#xff0c;如果扇出的链路上某个微服务的调用响…

配置python的opencv环境

1、核心 opencv与numpy包的版本必须适配。核心包版本&#xff1a; numpy1.15.0 opencv4.1.0 2、创建opencv环境 (1)创建一个opencv的虚拟环境 conda create -n cv_python python3.6.0(2)激活虚拟环境 activate cv_python (3)安装numpy的1.15.0版本 pip install numpy1.1…

windows自定时间显示格式(将星期几调出来)

一般的情况下是不是显示星期几的 添加红色方框中的内容

ctfshow 愚人杯菜狗杯部分题目(flasksession伪造ssti)

目录 <1>愚人杯 (1) easy_signin (2) easy_ssti(无过滤ssti) (3) easy_flask(flash-session伪造) (4) easy_php(C:开头序列化数据) <2> 菜狗杯 (1) 抽老婆(flask_session伪造) (2) 一言既出&#xff0c;驷马难追(intval) (3) 传说之下&#xff08;js控制台&…

0101quick_find_union-union_find-动态连通性-算法研究

文章目录1 前言2 动态连通性3 算法3.1 算法设计3.1 union-find算法API3.2 数据结构和通用实现3.3 quick-find算法3.3.1 思想和实现3.3.2 分析3.4 quick-union算法3.4.1 算法描述3.4.2 算法实现3.4.3 性能分析结语1 前言 为了说明我们设计和分析算法的基本方法&#xff0c;我们…

通过SSD对齐功能轻松将 HDD 克隆到 SSD

由于 SSD 的优越性能&#xff0c;越来越多的用户正在考虑将他们的操作系统从 HDD 转移到 SSD。然而&#xff0c;一些用户反馈&#xff0c;在迁移之后&#xff0c;SSD 的启动速度并未提升。 为什么将操作系统从 HDD &#xff08;机械硬盘&#xff09;迁移到 SSD&#xff08;固态…