使用Stomp PHP库连接到Spring websockets (Stomp)


Connect with a Stomp PHP library to Spring websockets (stomp)

我已经编译并运行了本指南中的代码:

http://spring.io/guides/gs/messaging-stomp-websocket/

我想使用这个Stomp PHP库连接到服务器

在本地主机的8080端口启动。

这是我运行连接到Spring服务器的代码:

<?php
// include a library
require_once("Stomp.php");
// make a connection
$con = new Stomp("tcp://localhost:8080");
// connect
$con->connect();
// send a message to the queue
$con->send("/hello", "test");
echo "Sent message with body 'test''n";
// subscribe to the queue
$con->subscribe("/topic/greetins");
// receive a message from the queue
$msg = $con->readFrame();
// do what you want with the message
if ( $msg != null) {
    echo "Received message with body '$msg->body''n";
    // mark the message as received in the queue
    $con->ack($msg);
} else {
    echo "Failed to receive a message'n";
}
// disconnect
$con->disconnect();

代码将在尝试接收响应的while循环中结束从服务器,在文件:Stomp.php试图接收帧。

服务器的响应是下一个:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Fri, 08 Aug 2014 18:11:23 GMT
Connection: close

我做错了什么?

这是supertom.com关于使用PHP Stomp的快速入门指南:http://www.supertom.com/code/activemq-php-stomp-quickstart.html (WebArchive)。

创建new Stomp()时,需要传递TCP套接字URL。我猜你的URL看起来是一个HTTP URL。例如,如果你连接到ActiveMQ,那么默认的URL是tcp://localhost:61613

那么第二行代码就是

$con = new Stomp("tcp://localhost:61613");