aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-05-27 16:45:59 +0300
committerGravatar Vitaly Takmazov2018-05-27 16:50:16 +0300
commit15c52d91884988dc3c714739f901b510eb6ffc56 (patch)
tree8e9c690c7c6d227fb6bd80d6cf061f0c58d0c9f9 /juick-server/src
parent7f181f590fbc7b2062a5348a3b050d7cf884a135 (diff)
ws: again, accept commands
Diffstat (limited to 'juick-server/src')
-rw-r--r--juick-server/src/main/java/com/juick/server/WebsocketManager.java15
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java5
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java2
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java22
4 files changed, 41 insertions, 3 deletions
diff --git a/juick-server/src/main/java/com/juick/server/WebsocketManager.java b/juick-server/src/main/java/com/juick/server/WebsocketManager.java
index bcb17bf1..85950c2c 100644
--- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java
+++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java
@@ -20,11 +20,13 @@ package com.juick.server;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.User;
import com.juick.server.helpers.AnonymousUser;
+import com.juick.server.helpers.CommandResult;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -34,6 +36,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
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.ConcurrentWebSocketSessionDecorator;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@@ -148,6 +151,18 @@ public class WebsocketManager extends TextWebSocketHandler {
}
});
}
+
+ @Override
+ protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
+ User user = clients.stream().filter(c -> c.session.equals(session)).map(c -> c.visitor)
+ .findFirst().orElse(AnonymousUser.INSTANCE);
+ String command = message.getPayload().trim();
+ if (StringUtils.isNotEmpty(command)) {
+ CommandResult result = commandsManager.processCommand(user, command, URI.create(""));
+ session.sendMessage(new TextMessage(result.getText()));
+ }
+ }
+
public List<SocketSubscribed> getClients() {
return clients;
}
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index 8e3e5dfd..9a78eac9 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -57,6 +57,9 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.socket.TextMessage;
+import org.springframework.web.socket.client.WebSocketConnectionManager;
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.w3c.dom.Document;
@@ -841,6 +844,8 @@ public class ServerTests {
WebsocketClientConfig.class, PropertyPlaceholderAutoConfiguration.class)
.run("--spring.main.web_environment=false");
long count = context.getBean(WebsocketClientConfig.class).getLatch().getCount();
+ WebsocketClientHandler handler = context.getBean(WebsocketClientHandler.class);
+ assertThat(handler.getLastMessage(), is("PONG"));
context.close();
Assert.assertThat(count, equalTo(0L));
mockMvc.perform(get("/ws/_all")).andExpect(status().isBadRequest());
diff --git a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
index 6be8e3c6..074be9b0 100644
--- a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
+++ b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientConfig.java
@@ -28,7 +28,7 @@ public class WebsocketClientConfig implements CommandLineRunner {
public WebSocketConnectionManager connectionManager() {
WebSocketConnectionManager manager = new WebSocketConnectionManager(client(),
handler(),
- "ws://localhost:8080/ws/");
+ "ws://localhost:8080/ws/_all");
manager.setAutoStartup(true);
return manager;
}
diff --git a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
index 7d896a04..ae92ccf0 100644
--- a/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
+++ b/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
@@ -1,18 +1,36 @@
package com.juick.server.tests;
+import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
+import java.io.IOException;
import java.util.concurrent.CountDownLatch;
class WebsocketClientHandler extends TextWebSocketHandler {
private final CountDownLatch latch;
+ private WebSocketSession session;
+ private String lastMessage;
public WebsocketClientHandler(CountDownLatch latch) {
this.latch = latch;
}
@Override
- public void afterConnectionEstablished(WebSocketSession session) throws Exception {
- super.afterConnectionEstablished(session);
+ public void afterConnectionEstablished(WebSocketSession session) throws IOException {
+ this.session = session;
+ session.sendMessage(new TextMessage("PING"));
+ }
+
+ @Override
+ protected void handleTextMessage(WebSocketSession session, TextMessage message) {
+ this.lastMessage = message.getPayload();
latch.countDown();
}
+
+ public WebSocketSession getSession() {
+ return session;
+ }
+
+ public String getLastMessage() {
+ return lastMessage;
+ }
}