简体   繁体   English

使用Stomp PHP库连接到Spring WebSockets

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

I've compiled and ran the code from this guide: 我已经编译并运行了本指南中的代码:

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

I want to connect using this Stomp PHP library to the server which is started on port 8080 on localhost. 我想使用 Stomp PHP库连接到在本地主机的端口8080上启动的服务器。

This is the code which I run to connect to Spring server: 这是我运行以连接到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();

The code will end up in a while loop trying to receive the response from the server, in file: Stomp.php trying to receive the frame. 该代码将最终在while循环中尝试接收来自服务器的响应,该文件位于Stomp.php文件中,试图接收该帧。

The response from the server is next: 接下来是服务器的响应:

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

What I'm doing wrong? 我做错了什么?

Here is a quick start guide from supertom.com for using PHP Stomp: http://www.supertom.com/code/activemq-php-stomp-quickstart.html (WebArchive). 这是来自supertom.com的使用PHP Stomp的快速入门指南: http : //www.supertom.com/code/activemq-php-stomp-quickstart.html (WebArchive)。

When you create a new Stomp() you need to pass a TCP socket URL. 创建new Stomp() ,需要传递一个TCP套接字URL。 I'm guessing that your URL looks is an HTTP URL. 我猜您的网址看起来是HTTP URL。 For example if you're connecting to ActiveMQ then the default URL is tcp://localhost:61613 . 例如,如果您要连接到ActiveMQ,则默认URL为tcp://localhost:61613

So you're second line of code would be 所以你的第二行代码是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM