diff options
author | 2018-03-16 10:46:23 +0300 | |
---|---|---|
committer | 2018-03-16 10:46:23 +0300 | |
commit | 9ef168a03b75aeca0c2f7dda9ce87d4014c703a9 (patch) | |
tree | 8ace0ada0f576d97592f8bc84803a2dcf3e7a244 /juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java | |
parent | 7cadae166b103182d7d1daaafe602cd8fb145c53 (diff) |
merge common projects
Diffstat (limited to 'juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java')
-rw-r--r-- | juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java | 71 |
1 files changed, 0 insertions, 71 deletions
diff --git a/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java b/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java deleted file mode 100644 index 792f446f..00000000 --- a/juick-server-web/src/main/java/com/juick/server/component/JuickServerComponent.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.juick.server.component; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.PingMessage; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.handler.TextWebSocketHandler; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - -public class JuickServerComponent extends TextWebSocketHandler { - private static Logger logger = LoggerFactory.getLogger(JuickServerComponent.class); - @Inject - private ApplicationEventPublisher applicationEventPublisher; - @Inject - private ObjectMapper jsonMapper; - - private WebSocketSession session; - private final AtomicBoolean closeFlag = new AtomicBoolean(false); - - @PostConstruct - public void init() { - closeFlag.set(false); - } - @Override - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - logger.info("WebSocket connected"); - this.session = session; - } - - @Override - public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { - logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason()); - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); - } - - @Override - protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception { - com.juick.Message jmsg = jsonMapper.readValue(text.asBytes(), com.juick.Message.class); - - if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled - logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg)); - if (!closeFlag.get()) { - applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); - } - } - - @Scheduled(fixedRate = 30000, initialDelay = 30000) - public void ping() throws IOException { - if (session != null && session.isOpen()) { - logger.debug("Sending WebSocket ping"); - session.sendMessage(new PingMessage()); - } else if (!closeFlag.get()) { - applicationEventPublisher.publishEvent(new DisconnectedEvent(this)); - } - } - @PreDestroy - public void close() { - closeFlag.set(true); - } - -} |