aboutsummaryrefslogtreecommitdiff
path: root/juick-ws
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-01-17 16:16:16 +0300
committerGravatar Vitaly Takmazov2017-01-17 16:16:16 +0300
commite901d21f98f9beb34d3322bf1f1902dd76343295 (patch)
tree4f0fd4bc5a19919756bd0ebc50f0e2548ba4d672 /juick-ws
parente39440a08a194eeb1a3e9513037d6bd3f4b8a3e1 (diff)
drop juick-console, add protocol handler to juick-ws
Diffstat (limited to 'juick-ws')
-rw-r--r--juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java21
-rw-r--r--juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java7
2 files changed, 28 insertions, 0 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
index 81f636a7..2f998298 100644
--- a/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
+++ b/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
@@ -1,9 +1,11 @@
package com.juick.ws;
import com.juick.User;
+import com.juick.server.protocol.JuickProtocol;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
@@ -13,11 +15,13 @@ import org.springframework.http.HttpHeaders;
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.inject.Inject;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
@@ -35,6 +39,8 @@ public class WebsocketComponent extends TextWebSocketHandler {
UserService userService;
@Inject
MessagesService messagesService;
+ @Inject
+ JuickProtocol protocol;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
@@ -130,6 +136,21 @@ public class WebsocketComponent extends TextWebSocketHandler {
});
}
+ @Override
+ protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
+ getClients().stream().filter(s -> !s.legacy && s.session.equals(session)).forEach(s -> {
+ User user = s.visitor;
+ String input = message.getPayload();
+ if (StringUtils.isNotBlank(input)) {
+ try {
+ s.session.sendMessage(new TextMessage(protocol.getReply(user, input).getResult()));
+ } catch (IOException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
+ logger.error("protocol exception", e);
+ }
+ }
+ });
+ }
+
public List<SocketSubscribed> getClients() {
return clients;
}
diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
index e0a31b13..b7e0e658 100644
--- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
+++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java
@@ -1,8 +1,10 @@
package com.juick.ws.configuration;
+import com.juick.server.protocol.JuickProtocol;
import com.juick.ws.WebsocketComponent;
import com.juick.ws.XMPPConnection;
import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@@ -19,6 +21,7 @@ import javax.inject.Inject;
* Created by aalexeev on 11/24/16.
*/
@Configuration
+@ComponentScan(basePackages = {"com.juick.server.protocol"})
@EnableScheduling
@EnableWebSocket
@PropertySource("classpath:juick.conf")
@@ -35,6 +38,10 @@ class WebsocketConfiguration implements WebSocketConfigurer {
public XMPPConnection ws() {
return new XMPPConnection(env, wsHandler());
}
+ @Bean
+ public JuickProtocol protocol() {
+ return new JuickProtocol("https://juick.com/");
+ }
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {