美文网首页
rabbitmq 给H5端发送消息

rabbitmq 给H5端发送消息

作者: 软件开发01 | 来源:发表于2022-01-07 16:47 被阅读0次

    docker安装rabbitmq

    docker pull rabbitmq
    docker run -d --hostname rmq --name rabbit -p 15672:15672 -p 5672:5672 -p 15670:15670 -p  15674:15674 rabbitmq
    
    ## 说明
    ## 默认账号/密码: guest/guest
    ## 15672-管理端口,5672-消息链接端口,15670-stomp_examples端口,15674-stomp服务端口
    
    

    rabbitmq打开插件stomp

    docker ps 
    docker exec -it  容器ID /bin/bash
    rabbitmq-plugins enable rabbitmq_management
    
    rabbitmq-plugins enable rabbitmq_management rabbitmq_web_stomp rabbitmq_stomp rabbitmq_web_stomp_examples
    
    ## 查看插件列表
    rabbitmq-plugins list
    

    H5端订阅mq

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>rmq-userid_51218</title>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
    
        <script src="https://cdn.bootcdn.net/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
    </head>
    <body>
        <h1>userid_51218 单独队列</h1>
        <div id="msg"></div>
        <script>
            //初始化对象
            var ws = new WebSocket('ws://192.168.3.163:15674/ws');
    
            // 获得Stomp client对象
            var client = Stomp.over(ws);
    
            // SockJS does not support heart-beat: disable heart-beats
            client.heartbeat.outgoing = 10000;
            client.heartbeat.incoming = 10000;
            //client.debug = pipe('#second');
    
            // 定义连接成功回调函数
            var on_connect = function (x) {
                //data.body是接收到的数据
                client.subscribe("/queue/userid_51218", function (data) {
                    var msg = data.body;
                    var html = $("#msg").html() + "<br />" + moment().format('LTS') + ":" + msg;
                    $("#msg").html(html);
                });
            };
    
            // 定义错误时回调函数
            var on_error = function () {
                console.log('error');
            };
    
            // 连接RabbitMQ
            client.connect('guest', 'guest', on_connect, on_error, '/');
            console.log(">>>连接上http://192.168.3.163:15674");
    
        </script>
    </body>
    </html>
    

    测试给客户端发送消息

    //get方法
    http://localhost:60518/api/Send/Msg?userid=51218&msg=123试试
    

    相关文章

      网友评论

          本文标题:rabbitmq 给H5端发送消息

          本文链接:https://www.haomeiwen.com/subject/nwwlcrtx.html