接口订阅

接口需要开发人员有前端技术能力

此处的接口地址请参看说明页面

步骤1、引入js文件

<script src="rabbitmq/sockjs-0.3.js">
<script src="rabbitmq/stomp.js">

js文件下载 rabbitmq.ziprabbitmq.zip

步骤2、建立连接

var has_had_focus = false;
var reconnectInterval=1000;
var timeoutInterval = 2000;
var pipe = function(el_name, send) {

var print = function(m, p) {

	//add log
	if(m=="Whoops! Lost connection to undefined"){
		//消息服务器链接失败业务提示
		//console.log(m);
	}

	if(m.indexOf("CONNECTED")>-1){
		//准备链接业务提示
		//console.log(m);
	}

	if(m.indexOf("connected to server")>-1){
	 	//链接成功业务提示
		// console.log(m);
	}

	if(m.indexOf("ERROR")>-1){
		if(m.indexOf("NOT_FOUND")>-1){
			//消息通道未建立业务提示
		}
		//console.log(m);
	}

	if(m.indexOf("PING")>-1){
		console.log('心跳 101'); //心跳
	}

	if(m.indexOf("SUBSCRIBE")>-1){
		//订阅成功业务提示
	}

	return print;
};

var print_first = pipe('#first', function(data) {
//    client.send('/topic/test', {"content-type":"text/plain"}, data);
});

 var routingkey = 'xxxxxx';#接口获取
 var ws = new WebSocket("接口地址");
 // Init Client
 var client = Stomp.over(ws);

// SockJS does not support heart-beat: disable heart-beats
client.heartbeat.outgoing = 59000;
client.heartbeat.incoming = 0;
client.debug = pipe('#second');

// Declare on_connect
var on_connect = function(x) {
//订阅消息
client.subscribe("/exchange/weifu_pop/"+routingkey, function(message) {
	var content = message.body;
	console.log(content);
	var obj = JSON.parse(content);
	//业务需求实现可在这里编写
	});
};

// Declare on_error
var on_error =  function(msg) {
	console.log('error:'+msg);
};

// Conect to RabbitMQ
client.connect('厂家申请', '厂家申请', on_connect, on_error, '/');