我尝试通过Spring WebSocket实现向特定用户发送消息。
来自控制器的方法
simpMessagingTemplate.convertAndSend("/user/" + username + "/queue/messages", messageDto);
但是如何获得用户名?当我搜索需要将DefaultHandshakeHandler添加到websocket端点
时我已添加:
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/chat" allowed-origins="*">
<websocket:handshake-handler ref="customHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue" />
public class CustomDefaultHandshakeHandler extends DefaultHandshakeHandler {
@Override
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
But attributes is null
.....
return principal;
}
}
例如:Sending message to specific user using spring
String name = (String)attributes.get("name");
但属性为空(null)。 我该怎么做才能获得用户名属性? 前端使用Angular4并踩踏WebSocket服务
this.stomp.configure({
host: AppSettings.WEBSOCKET_HOST + '/chat',
headers: {'username': 'test'},
queue:{'init':false}
});