ebc

ipu-stomp.js 3.6KB

    import SockJS from 'sockjs-client' import Stomp from 'stompjs' // import UUID from './uuid' // define(["sockjs", "stomp", "uuid"],function(SockJS, Stomp, UUID) { // var IpuStomp = {}; export default function (params, callbacks) { var onConnected, onFailure if (callbacks) { /* 供stomp.js中做判断:args[1] instanceof Function */ onConnected = callbacks.onConnected ? callbacks.onConnected : new Function() onFailure = callbacks.onFailure ? callbacks.onFailure : new Function() } else { onConnected = new Function() onFailure = new Function() } var client if (params.url.startsWith('http') || params.url.startsWith('https')) { // SockJS支持http和https前缀 var socket = new SockJS(params.url) client = Stomp.over(socket) } else { // SockJS支持ws前缀 client = Stomp.client(params.url) } var headers = {'name': params.name, 'passcode': params.passcode, 'host': params.host, 'uuid': params.UUID} client.connect(headers, onConnected, onFailure) return new StompClient(client) } // IpuStomp.connect = function(url, login, passcode, callbacks, host){ // if(url.startsWith("http")||url.startsWith("https")){ // return connectWithSockJS(url, login, passcode, callbacks, host); //SockJS支持http和https前缀 // }else { // return connectWithStomp(url, login, passcode, callbacks, host); //SockJS支持ws前缀 // } // } // // function connectWithStomp(url, login, passcode, callbacks, host){ // var onConnected, onFailure; // if(callbacks){ // /*供stomp.js中做判断:args[1] instanceof Function*/ // onConnected = callbacks.onConnected?callbacks.onConnected:new Function(); // onFailure = callbacks.onFailure?callbacks.onFailure:new Function(); // }else{ // onConnected = new Function(); // onFailure = new Function(); // } // var client = Stomp.client(url); // var headers = {"login":login,"passcode":passcode,"host":host,"uuid":UUID}; // client.connect(headers, onConnected, onFailure); // return new StompClient(client); // } // // function connectWithSockJS(url, login, passcode, callbacks, host){ // var onConnected, onFailure; // if(callbacks){ // /*供stomp.js中做判断:args[1] instanceof Function*/ // onConnected = callbacks.onConnected?callbacks.onConnected:new Function(); // onFailure = callbacks.onFailure?callbacks.onFailure:new Function(); // }else{ // onConnected = new Function(); // onFailure = new Function(); // } // // var socket = new SockJS(url); // var client = Stomp.over(socket); // var headers = {"login":login,"passcode":passcode,"host":host,"uuid":UUID}; // client.connect(headers, onConnected, onFailure); // return new StompClient(client); // } function StompClient(client) { this.client = client } StompClient.prototype.setTopicPath = function(topicPath) { // topicPath = topicPath.startsWith('/') ? topicPath : '/' + topicPath // topicPath = topicPath.endsWith('/') ? topicPath : topicPath + '/' // console.log(topicPath) this.topicPath = topicPath } StompClient.prototype.disconnect = function() { this.client.disconnect() } StompClient.prototype.connected = function() { return this.client.connected } StompClient.prototype.subscribe = function(topic, callback) { // return this.client.subscribe(this.topicPath + topic, callback) return this.client.subscribe(this.topicPath, callback) } StompClient.prototype.publish = function(topic, text) { // this.client.send(this.topicPath + topic, {}, text) this.client.send(this.topicPath, {}, text) } // return IpuStomp; // }) // export default { // IpuStomp: { // connect:IpuStomp.connect // } // }