ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

sockjs+stomp的websocket插件

2019-11-25 15:06:42  阅读:319  来源: 互联网

标签:function 插件 sockjs stompClient stomp ws eventType WS data


/**
 * 依赖文件sockjs.js、stomp.js
 * */
;!(function (window) {
    let WS = function () {
        this.isConnect = false;
        //保存所有的订阅事件 {Aevent:[pubfun(status,data),pubfun(status,data),...]}
        this.subEvents = {};
        this.stompClient = null;
    };

    WS.prototype = {
        constructor: WS
        //设置连接状态
        , setConnect(status) {
            this.isConnect = status;
        }
        //建立连接
        , connect(url) {
            let ws = new SockJS(url)
                , stompClient = Stomp.over(ws);
            stompClient.connect({}, (data) => {
                this.setConnect(true);
                this.connectSuc.apply(this, [stompClient, data]);
            }, (error) => {
                this.setConnect(false);
                this.connectErr.apply(this,[stompClient,error]);
            });
            this.stompClient = stompClient;
            return stompClient;
        }
        //断开连接
        , disconnect: function () {
            if(this.stompClient != null && this.isConnect) {
                this.isConnect = false;
                this.stompClient.disconnect();
            }
        }
        //连接成功后的回调
        , connectSuc(stompClient, data) {
            if(this.isConnect){
                //发布连接成功事件
                this.trigger.apply(this, ['connectSuc', stompClient, data]);
                //发布发送消息到服务端事件
                this.trigger.apply(this, ['sendMessage', stompClient, data]);
            }
        }
        //连接失败后的回调
        , connectErr(stompClient, data){
            //发布连接失败事件
            this.trigger.apply(this, ['connectErr', stompClient, data]);
        }
        //发布函数
        , trigger(eventType, ...data) {
            eventType = this.subEvents[eventType];
            for (var i = 0; i < eventType.length; i++) {
                eventType[i].apply(this, data);
            }
        }
        //订阅方法 --->用于订阅指定事件
        , on(eventType, handle) {
            if (!(eventType in this.subEvents)) {
                this.subEvents[eventType] = [];
            }
            this.subEvents[eventType].push(handle);
        }
        //删除订阅
        , off(eventType, handle) {
            eventType = this.subEvents[eventType];
            if (eventType) {
                let handleIndex = eventType.indexOf(handle);
                if (handleIndex >= 0) {
                    eventType.splice(handleIndex, 1);
                }
            }
        }
    };

    window.WS = WS;
})(window);


/**
 *
 *  var ws = new WS();
 *  ws.connect("/helloWebsocket");
    ws.on('connectSuc',function (stompClient,data) {
        stompClient.subscribe('/topic/serverSend',function (response) {
            info.innerHTML += "<div>"+response+"</div>";
        });
    });

   ws.on('connectErr',function (stompClient,data) {
        stompClient.subscribe('/topic/serverSend',function (response) {
            info.innerHTML += "<div>"+response+"</div>";
        });
    });

    ws.on('sendMessage',function (stompClient,data) {
        stompClient.send("/client/clientSendMessage",{},"hello server !!");
    });

    //强制关闭窗口后,断开连接
    window.onunload = function (ev) {
       ws.disconnect();
    }
 *
 * */

 

标签:function,插件,sockjs,stompClient,stomp,ws,eventType,WS,data
来源: https://www.cnblogs.com/littleboyck/p/11927810.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有