Simple: convertAndSendToUser Where do I get a username?

北慕城南 提交于 2019-12-03 08:47:41

This answer is written based on this application: https://github.com/spring-guides/gs-messaging-stomp-websocket

In order to register a user, you must first create an object that will represent it, for example:

public final class User implements Principal {

    private final String name;

    public User(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }
}

Then you'll need a way to create these User objects. One way of doing it is when SockJS sends you the connect message headers. In order to do so, you need to intercept the connect message. You can do that by creating our your interceptor, for example:

public class UserInterceptor extends ChannelInterceptorAdapter {

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {

        StompHeaderAccessor accessor =
                MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);

        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            Object raw = message
                    .getHeaders()
                    .get(SimpMessageHeaderAccessor.NATIVE_HEADERS);

            if (raw instanceof Map) {
                Object name = ((Map) raw).get("name");

                if (name instanceof LinkedList) {
                    accessor.setUser(new User(((LinkedList) name).get(0).toString()));
                }
            }
        }
        return message;
    }
}

Once you have that, you must also register this UserInterceptor. I'm guessing somewhere in your application you have defined a configuration AbstractWebSocketMessageBrokerConfigurer class. In this class you can register your user interceptor by overriding configureClientInboundChannel method. You can do it like this:

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
    registration.setInterceptors(new UserInterceptor());
}

And then finally, when your clients connect, they'll have to provide their usernames:

stompClient.connect({
    name: 'test' // Username!
}, function () {
    console.log('connected');
});

After you have all this setup, simpUserRegistry.getUsers() will return a list of users and you'll be able to use convertAndSendToUser method:

messaging.convertAndSendToUser("test", ..., ...);

Edit

Testing this out a bit further, when subscribing, you'll have to prefix your topics with /user as SimpMessagingTemplate uses this as a default prefix, for example:

stompClient.subscribe('/user/...', ...);

Also I had made a mistake in UserInterceptor and corrected it (name parsing part).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!