aboutsummaryrefslogtreecommitdiff
path: root/juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java')
-rw-r--r--juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java21
1 files changed, 21 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;
}