春季会议+春季Web套接字.根据会话ID向特定客户端发送消息 [英] Spring session + Spring web socket. Send message to specific client based on session id

查看:161
本文介绍了春季会议+春季Web套接字.根据会话ID向特定客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 Quetion1 Quetion2 根据其sessionId将消息发送到特定客户端,但不能找到成功.

I have followed Quetion1 and Quetion2 from stack overflow to send messages to specific client, based on its sessionId but could not find success.

下面是我的示例RestController类

Below is my sample RestController class

@RestController
public class SpringSessionTestApi {

@Autowired
public SimpMessageSendingOperations messagingTemplate;

@MessageMapping("/messages")
public void greeting(HelloMessage message, SimpMessageHeaderAccessor headerAccessor) throws Exception {

    String sessionId  = (String) headerAccessor.getSessionAttributes().get("SPRING.SESSION.ID");
    messagingTemplate.convertAndSendToUser(sessionId,"/queue/test",message, createHeaders(sessionId));

   }

private MessageHeaders createHeaders(String sessionId) {
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    headerAccessor.setSessionId(sessionId);
    headerAccessor.setLeaveMutable(true);
    return headerAccessor.getMessageHeaders();
   }
}

会话ID:当客户端发送createSession请求时,将生成新的spring sessionId并将其存储在MongoDB中.之后,当客户端发送Web套接字连接请求时,接收到相同的sessionId,该会话ID如预期那样存储在mongoDb中.直到一切正常为止.

Session Id: when client sends createSession request, new spring sessionId is generated and same is stored in MongoDB as well. After that when client sends web socket connect request, same sessionId is received which was stored in mongoDb as expected. Till This everything is working fine.

现在,我的工作是根据sessionId将响应发送回客户端. 为此,我有下面的Web套接字类:

Now my job is to send response back to the client based on the sessionId. For that I have below web socket class:

@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends
    AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {

@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/messages");
}

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

和我用来连接的示例客户端代码是:

and the sample client code that I am using to connect is:

function connect() {

stompClient = Stomp.client('ws://localhost:8016/messages');
stompClient.debug = null;

stompClient.connect({}, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('/user/queue/test', function (greeting) {
        console.log("Hello "+greeting);
        console.log("Greeting body "+JSON.parse(greeting.body));

    });
});
}

请帮助,我在哪里做错了? 在此先感谢!

Please help, Where I am doing wrong in this? Thanks in Advance!

推荐答案

我在git中找到了一个完全可用的Spring Stomp Chat项目,链接在这里.您可以参考它. https://gist.github.com/theotherian/9906304

I've found a full workable Spring Stomp Chat project in git, the link is here. You can refer to it. https://gist.github.com/theotherian/9906304

这篇关于春季会议+春季Web套接字.根据会话ID向特定客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆