mirror of
https://github.com/anthonyraymond/joal.git
synced 2024-11-10 17:12:36 +08:00
Add message sender for ClientHasStarted
This commit is contained in:
parent
75edea11f6
commit
42c5735acd
2 changed files with 71 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue