连接Stomp PHP库到Spring websockets(stomp)

时间:2014-08-08 15:12:25

标签: php spring websocket stomp

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

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

我想使用this Stomp PHP库连接到服务器 在localhost上的端口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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

以下是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");