use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-integration by spring-projects.
the class TestServerConfig method clientOutboundChannel.
@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
DirectChannel directChannel = new DirectChannel();
directChannel.addInterceptor(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
headers.setLeaveMutable(true);
return MessageBuilder.createMessage(message.getPayload(), headers.getMessageHeaders());
}
});
return directChannel;
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project BootForum by chipolaris.
the class WebSocketEventsListener method handleSessionUnsubscribeEvent.
@EventListener
private void handleSessionUnsubscribeEvent(SessionUnsubscribeEvent event) {
Message<byte[]> message = event.getMessage();
// https://stackoverflow.com/questions/54330744/spring-boot-websocket-how-to-get-notified-on-client-subscriptions
SimpMessageHeaderAccessor simpMessageHeaderAccessor = SimpMessageHeaderAccessor.wrap(message);
String username = "Anonymous";
Principal user = event.getUser();
if (user != null) {
username = user.getName();
}
String subscriptionId = simpMessageHeaderAccessor.getSubscriptionId();
Map<String, Object> sessionAttributes = simpMessageHeaderAccessor.getSessionAttributes();
String simpDestination = (String) sessionAttributes.get(subscriptionId);
boolean removeResult = chatManager.removeSubscribedUser(simpDestination, username);
if (removeResult) {
logger.info(String.format("User %s unsubscribes from simpDestination: %s", username, simpDestination));
this.template.convertAndSend(simpDestination, new RoomMessage(username, "leave", System.currentTimeMillis(), null));
} else {
logger.info(String.format("A session from user %s unsubscribes to simpDestination: %s", username, simpDestination));
}
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project activiti-cloud by Activiti.
the class GraphQLBrokerMessageHandlerTest method simpHeaderAccessor.
private SimpMessageHeaderAccessor simpHeaderAccessor(WebSocketSession session) {
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headerAccessor.setDestination(destination);
headerAccessor.setSessionId(session.getId());
headerAccessor.setSessionAttributes(session.getAttributes());
headerAccessor.setUser(session.getPrincipal());
headerAccessor.setLeaveMutable(true);
return headerAccessor;
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project activiti-cloud by Activiti.
the class GraphQLBrokerMessageHandlerTest method connectionInitMessage.
private Message<GraphQLMessage> connectionInitMessage(String operationId, String sessionId) {
SimpMessageHeaderAccessor headerAccessor = simpHeaderAccessor(mockWebSocketSession(sessionId));
headerAccessor.setHeader(StompHeaderAccessor.HEART_BEAT_HEADER, new long[] { 0, 5000 });
GraphQLMessage payload = new GraphQLMessage(operationId, GraphQLMessageType.CONNECTION_INIT);
return MessageBuilder.createMessage(payload, headerAccessor.getMessageHeaders());
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project activiti-cloud by Activiti.
the class GraphQLBrokerSubProtocolHandler method handleMessageFromClient.
@Override
public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> message, MessageChannel outputChannel) throws Exception {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
GraphQLMessage sourceMessage = objectMapper.reader().forType(GraphQLMessage.class).readValue(textMessage.getPayload());
try {
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headerAccessor.setDestination(destination);
headerAccessor.setSessionId(session.getId());
headerAccessor.setSessionAttributes(session.getAttributes());
headerAccessor.setUser(getUser(session));
headerAccessor.setLeaveMutable(true);
Message<GraphQLMessage> decodedMessage = MessageBuilder.createMessage(sourceMessage, headerAccessor.getMessageHeaders());
headerAccessor.setHeader(GRAPHQL_MESSAGE_TYPE, sourceMessage.getType().toString());
if (logger.isTraceEnabled()) {
logger.trace("From client: " + headerAccessor.getShortLogMessage(message.getPayload()));
}
boolean isConnect = GraphQLMessageType.CONNECTION_INIT.equals(sourceMessage.getType());
if (isConnect) {
this.stats.incrementConnectCount();
// Let's inject connectionParams into headers
Optional.ofNullable(sourceMessage.getPayload()).ifPresent(map -> {
map.entrySet().forEach(e -> {
headerAccessor.setHeader(e.getKey(), e.getValue());
});
});
// inject client KA interval
Integer kaInterval = Optional.ofNullable(headerAccessor.getHeader(KA_INTERVAL_HEADER)).map(v -> Integer.parseInt(v.toString())).orElse(DEFAULT_KA_INTERVAL);
headerAccessor.setHeader(StompHeaderAccessor.HEART_BEAT_HEADER, new long[] { 0, kaInterval });
} else if (GraphQLMessageType.CONNECTION_TERMINATE.equals(sourceMessage.getType())) {
this.stats.incrementDisconnectCount();
} else if (GraphQLMessageType.START.equals(sourceMessage.getType())) {
this.stats.incrementStartCount();
} else if (GraphQLMessageType.STOP.equals(sourceMessage.getType())) {
this.stats.incrementStopCount();
}
try {
SimpAttributesContextHolder.setAttributesFromMessage(decodedMessage);
boolean sent = outputChannel.send(decodedMessage);
if (sent) {
if (isConnect) {
Principal user = headerAccessor.getUser();
if (user != null && user != session.getPrincipal()) {
this.graphqlAuthentications.put(session.getId(), user);
}
}
if (this.eventPublisher != null) {
if (isConnect) {
publishEvent(new GraphQLSessionConnectEvent(this, decodedMessage, getUser(session)));
} else if (GraphQLMessageType.START.equals(sourceMessage.getType())) {
publishEvent(new GraphQLSessionSubscribeEvent(this, decodedMessage, getUser(session)));
} else if (GraphQLMessageType.STOP.equals(sourceMessage.getType())) {
publishEvent(new GraphQLSessionUnsubscribeEvent(this, decodedMessage, getUser(session)));
}
}
}
} finally {
SimpAttributesContextHolder.resetAttributes();
}
} catch (Throwable ex) {
if (logger.isErrorEnabled()) {
logger.error("Failed to send client message to application via MessageChannel" + " in session " + session.getId() + ". Sending CONNECTION_ERROR to client.", ex);
}
sendErrorMessage(session, ex, sourceMessage);
}
}
return;
}
Aggregations