使用StompClient在Spring WebSocket上传递消息时出错

时间:2014-04-09 03:39:26

标签: java spring websocket stomp spring-websocket

我正在尝试向我的主题发送消息,但问题是当我发送消息时没有任何反应...我正在使用apache tomcat 7.0.53

  

更新:04/15:链接到测试:

     

http://ec2-54-187-72-145.us-west-2.compute.amazonaws.com:8080/kupo

     

登录:admin

     

密码:admin

     

链接访问TOMCAT日志:

     

http://ec2-54-187-72-145.us-west-2.compute.amazonaws.com:28778/

     

P.S:您需要检查侧栏上的组合框以开始观看消息

Github链接:https://github.com/tiarebalbi/kupo

LOG:

DEBUG - gWebSocketHandlerDecorator - Connection established, SockJS session id=_mqg8qer, uri=/kupo/application/807/_mqg8qer/websocket
DEBUG - StompDecoder               - Decoded [Payload byte[0]][Headers=    {stompCommand=CONNECT, nativeHeaders={heart-beat=[10000,10000], accept-version=[1.1,1.0]},     simpMessageType=CONNECT, id=e79a615e-5522-a0f9-aecf-6ea5a54b3d9b, timestamp=1397013491497}]
DEBUG - StompEncoder               - Encoded STOMP command=CONNECTED headers={user-name=[balbi], heart-beat=[0,0], version=[1.1]}
DEBUG - StompDecoder               - Decoded [Payload byte[0]][Headers={stompCommand=SUBSCRIBE, nativeHeaders={id=[sub-0], destination=[/topic/greetings]}, simpMessageType=SUBSCRIBE, simpSubscriptionId=sub-0, simpDestination=/topic/greetings, id=42c2019d-96a0-95f0-29aa-2bcc62d6d721, timestamp=1397013491501}]

CODE:

@Service
public class ExampleServiceImpl implements ApplicationListener<BrokerAvailabilityEvent> {

private AtomicBoolean brokerAvailable = new AtomicBoolean();

@Autowired
private MessageSendingOperations<String> messagingTemplate;

@Override
public void onApplicationEvent(BrokerAvailabilityEvent event) {
    this.brokerAvailable.set(event.isBrokerAvailable());
}

@Scheduled(fixedDelay=3000)
public void testing() {
    if (this.brokerAvailable.get()) {
        this.messagingTemplate.convertAndSend("/topic/greetings", "Testing....");
    }
}

Javascript Connect:

        var socket = new SockJS('/kupo/application'); // <!-- My endpoint
        var stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {

            var username = frame.headers['user-name'];
            console.log("User connected: " + username);

            stompClient.subscribe("/topic/greetings", function(message) { // <-- Topic where I want to received the message
                console.log("TOPIC:",message);
            });

        } , function(error) {
            console.log("STOMP protocol error " + error);
        });

浏览器控制台:

Opening Web Socket... stomp.min.js:8
Web Socket Opened... stomp.min.js:8
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<< CONNECTED
user-name:balbi
heart-beat:0,0
version:1.1

connected to server undefined stomp.min.js:8
User connected: balbi 
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings

Browser Console

Websocket上下文配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketApplicationContext extends AbstractWebSocketMessageBrokerConfigurer {

@Autowired
private Environment env;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    if (env.acceptsProfiles("test.tomcat")) {
        registry.addEndpoint("/application")
                .setHandshakeHandler(
                        new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
                .withSockJS();
    } else {
        registry.addEndpoint("/application").withSockJS();
    }
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/queue/", "/topic/");
    registry.setApplicationDestinationPrefixes("/app");
}
}

1 个答案:

答案 0 :(得分:0)

连接到您的应用程序时,我设法从JavaScript控制台向主题发送消息,并在网页中收到消息。

var socket = new SockJS('/kupo/application');
var stompClient = Stomp.over(socket);
stompClient.send('/topic/greetings',{},"hello");

我收到了:

<<< MESSAGE
subscription:sub-0
content-length:5
message-id:ts3oov6b-1
destination:/topic/greetings
content-length:5

hello 

您的计划任务是否按预期调用?

为什么您在主要配置中importing some Configurations and still scanning the configuration package?难道不是一个或另一个吗?