sockjs+stomp的websocket插件

/**
 * 依赖文件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();
    }
 *
 * */

 

上一篇:基于stomp.py的持久客户端填充ActiveMQ中的订户列表


下一篇:python-Stomp.py如何从侦听器返回消息