diff options
author | Vitaly Takmazov | 2017-01-17 16:16:16 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-01-17 16:16:16 +0300 |
commit | e901d21f98f9beb34d3322bf1f1902dd76343295 (patch) | |
tree | 4f0fd4bc5a19919756bd0ebc50f0e2548ba4d672 /juick-ws/src/main/java/com/juick/ws/WebsocketComponent.java | |
parent | e39440a08a194eeb1a3e9513037d6bd3f4b8a3e1 (diff) |
drop juick-console, add protocol handler to juick-ws
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.java | 21 |
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; } |