php rabbitMq 与前端交互
1.搭建RabbitMq服务
2.开启amqp sockets bcmath 等需要的扩展
3.开启rabbitmq 前端插件
rabbitmq-plugins enable rabbitmq_management rabbitmq_web_stomp rabbitmq_stomp rabbitmq_web_stomp_examples
4.前端代码
<!DOCTYPE HTML>
<html>
<head>
<title>My WebSocket</title>
</head>
<body>
Welcome<br/>
<input id="text" type="text" />
<div id="message">
</div>
</body>
<script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
<script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.js"></script>
<script type="text/javascript">
if (typeof WebSocket == 'undefined') {
console.log('不支持websocket')
}
// 初始化 ws 对象
var ws = new WebSocket('ws://192.168.1.22:15674/ws');
// 获得Stomp client对象
var client = Stomp.over(ws);
// 定义连接成功回调函数
var on_connect = function(x) {
//data.body是接收到的数据
client.subscribe("/queue/queue", function(data) {
var msg = data.body;
console.log("收到数据:"+msg)
});
};
// 定义错误时回调函数
var on_error = function() {
console.log('error');
};
// 连接RabbitMQ
client.connect('admin', '123456', on_connect, on_error, '/');
console.log(">>>连接上http://localhost:5672");
</script>
</html>
5.添加用户admin 密码123456
6.在Queues下新建queue 并且绑定logs交换机