Add message sender for ClientHasStarted

This commit is contained in:
anthonyraymond 2017-06-22 22:57:52 +02:00
parent 75edea11f6
commit 42c5735acd
2 changed files with 71 additions and 0 deletions

View file

@ -0,0 +1,28 @@
package org.araymond.joal.web.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
/**
* Created by raymo on 22/06/2017.
*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
config.enableSimpleBroker("/global");
config.setApplicationDestinationPrefixes("/joal");
}
@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/joal-sock").withSockJS();
}
}

View file

@ -0,0 +1,43 @@
package org.araymond.joal.web.services;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
import org.araymond.joal.core.events.seedsession.SeedSessionHasStartedEvent;
import org.araymond.joal.web.messages.outgoing.impl.ClientHasStartedMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* Created by raymo on 22/06/2017.
*/
@Service
public class WebGlobalEventListener {
private static final Logger logger = LoggerFactory.getLogger(WebGlobalEventListener.class);
private final SimpMessageSendingOperations messagingTemplate;
public WebGlobalEventListener(final SimpMessageSendingOperations messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
@Order(Ordered.LOWEST_PRECEDENCE)
@EventListener
void handleSeedSessionHasStarted(final SeedSessionHasStartedEvent event) {
logger.debug("Send ClientHasStartedMessage to client.");
final String client = event.getBitTorrentClient().getHeaders().stream()
.map(Map.Entry::getKey)
.filter("User-Agent"::equalsIgnoreCase)
.findFirst()
.orElse("Unknown");
messagingTemplate.convertAndSend("/global", new ClientHasStartedMessage(client));
}
}