zhouhao hace 8 años
padre
commit
216c30a756

+ 1 - 3
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/WebSocketCommand.java

@@ -10,9 +10,7 @@ import java.util.Map;
  *
  * @author zhouhao
  */
-public interface WebSocketCommand {
-    String getCommand();
-
+public interface CommandRequest {
     Authentication getAuthentication();
 
     Map<String, Object> getParameters();

+ 10 - 15
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/handler/CommandWebSocketMessageDispatcher.java

@@ -4,11 +4,11 @@ import com.alibaba.fastjson.JSON;
 import com.fasterxml.jackson.core.JsonParseException;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.container.AuthenticationContainer;
-import org.hswebframework.web.socket.WebSocketCommand;
+import org.hswebframework.web.socket.CommandRequest;
 import org.hswebframework.web.socket.WebSocketSessionListener;
 import org.hswebframework.web.socket.message.WebSocketMessage;
-import org.hswebframework.web.socket.processor.WebSocketProcessor;
-import org.hswebframework.web.socket.processor.WebSocketProcessorContainer;
+import org.hswebframework.web.socket.processor.CommandProcessor;
+import org.hswebframework.web.socket.processor.CommandProcessorContainer;
 import org.springframework.util.StringUtils;
 import org.springframework.web.socket.CloseStatus;
 import org.springframework.web.socket.TextMessage;
@@ -23,7 +23,7 @@ import java.util.Map;
  */
 public class CommandWebSocketMessageDispatcher extends TextWebSocketHandler {
 
-    private WebSocketProcessorContainer processorContainer;
+    private CommandProcessorContainer processorContainer;
 
     private AuthenticationContainer authenticationContainer;
 
@@ -37,7 +37,7 @@ public class CommandWebSocketMessageDispatcher extends TextWebSocketHandler {
         this.authenticationContainer = authenticationContainer;
     }
 
-    public void setProcessorContainer(WebSocketProcessorContainer processorContainer) {
+    public void setProcessorContainer(CommandProcessorContainer processorContainer) {
         this.processorContainer = processorContainer;
     }
 
@@ -50,9 +50,9 @@ public class CommandWebSocketMessageDispatcher extends TextWebSocketHandler {
         String payload = message.getPayload();
         if (StringUtils.isEmpty(payload)) return;
         try {
-            CommandRequest request = JSON.parseObject(payload, CommandRequest.class);
-            WebSocketCommand command = buildCommand(request, session);
-            WebSocketProcessor processor = processorContainer.getProcessor(command.getCommand());
+            WebSocketCommandRequest request = JSON.parseObject(payload, WebSocketCommandRequest.class);
+            CommandRequest command = buildCommand(request, session);
+            CommandProcessor processor = processorContainer.getProcessor(request.getCommand());
             if (processor != null) {
                 processor.execute(command);
             } else {
@@ -71,13 +71,8 @@ public class CommandWebSocketMessageDispatcher extends TextWebSocketHandler {
         return WebSocketUtils.getAuthentication(authenticationContainer, socketSession);
     }
 
-    private WebSocketCommand buildCommand(CommandRequest request, WebSocketSession socketSession) {
-        return new WebSocketCommand() {
-            @Override
-            public String getCommand() {
-                return request.getCommand();
-            }
-
+    private CommandRequest buildCommand(WebSocketCommandRequest request, WebSocketSession socketSession) {
+        return new CommandRequest() {
             @Override
             public Authentication getAuthentication() {
                 return getAuthenticationFromSession(socketSession);

+ 1 - 1
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/handler/CommandRequest.java

@@ -7,7 +7,7 @@ import java.util.Map;
  *
  * @author zhouhao
  */
-public class CommandRequest {
+public class WebSocketCommandRequest {
     private String command;
 
     private Map<String, Object> parameters;

+ 3 - 3
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/processor/WebSocketProcessor.java

@@ -1,16 +1,16 @@
 package org.hswebframework.web.socket.processor;
 
-import org.hswebframework.web.socket.WebSocketCommand;
+import org.hswebframework.web.socket.CommandRequest;
 
 /**
  * TODO 完成注释
  *
  * @author zhouhao
  */
-public interface WebSocketProcessor {
+public interface CommandProcessor {
     String getName();
 
-    void execute(WebSocketCommand command);
+    void execute(CommandRequest command);
 
     void init();
 

+ 17 - 0
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/processor/CommandProcessorContainer.java

@@ -0,0 +1,17 @@
+package org.hswebframework.web.socket.processor;
+
+
+import java.util.List;
+
+/**
+ * @author zhouhao
+ */
+public interface CommandProcessorContainer {
+    CommandProcessor install(CommandProcessor command);
+
+    CommandProcessor uninstall(String name);
+
+    CommandProcessor getProcessor(String name);
+
+    List<CommandProcessor> getAllProcessor();
+}

+ 8 - 8
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/processor/DefaultWebSocketProcessorContainer.java

@@ -8,35 +8,35 @@ import java.util.concurrent.ConcurrentMap;
 /**
  * @author zhouhao
  */
-public class DefaultWebSocketProcessorContainer implements WebSocketProcessorContainer {
+public class DefaultCommandProcessorContainer implements CommandProcessorContainer {
 
-    private final ConcurrentMap<String, WebSocketProcessor> processorStore = new ConcurrentHashMap<>();
+    private final ConcurrentMap<String, CommandProcessor> processorStore = new ConcurrentHashMap<>();
 
     @Override
-    public WebSocketProcessor install(WebSocketProcessor command) {
+    public CommandProcessor install(CommandProcessor command) {
         command.init();
         return processorStore.put(command.getName(), command);
     }
 
     @Override
-    public WebSocketProcessor uninstall(String name) {
-        WebSocketProcessor processor = processorStore.remove(name);
+    public CommandProcessor uninstall(String name) {
+        CommandProcessor processor = processorStore.remove(name);
         if (null != processor) processor.destroy();
         return processor;
     }
 
     public void destroy() {
-        getAllProcessor().forEach(WebSocketProcessor::destroy);
+        getAllProcessor().forEach(CommandProcessor::destroy);
         processorStore.clear();
     }
 
     @Override
-    public WebSocketProcessor getProcessor(String name) {
+    public CommandProcessor getProcessor(String name) {
         return processorStore.get(name);
     }
 
     @Override
-    public List<WebSocketProcessor> getAllProcessor() {
+    public List<CommandProcessor> getAllProcessor() {
         return new ArrayList<>(processorStore.values());
     }
 }

+ 0 - 17
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/processor/WebSocketProcessorContainer.java

@@ -1,17 +0,0 @@
-package org.hswebframework.web.socket.processor;
-
-
-import java.util.List;
-
-/**
- * @author zhouhao
- */
-public interface WebSocketProcessorContainer {
-    WebSocketProcessor install(WebSocketProcessor command);
-
-    WebSocketProcessor uninstall(String name);
-
-    WebSocketProcessor getProcessor(String name);
-
-    List<WebSocketProcessor> getAllProcessor();
-}

+ 11 - 11
hsweb-message/hsweb-message-websocket/src/main/java/org/hswebframework/web/socket/starter/CommandWebSocketAutoConfiguration.java

@@ -6,9 +6,9 @@ import org.hswebframework.web.socket.WebSocketSessionListener;
 import org.hswebframework.web.socket.handler.CommandWebSocketMessageDispatcher;
 import org.hswebframework.web.socket.message.DefaultWebSocketMessager;
 import org.hswebframework.web.socket.message.WebSocketMessager;
-import org.hswebframework.web.socket.processor.DefaultWebSocketProcessorContainer;
-import org.hswebframework.web.socket.processor.WebSocketProcessor;
-import org.hswebframework.web.socket.processor.WebSocketProcessorContainer;
+import org.hswebframework.web.socket.processor.DefaultCommandProcessorContainer;
+import org.hswebframework.web.socket.processor.CommandProcessor;
+import org.hswebframework.web.socket.processor.CommandProcessorContainer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -28,16 +28,16 @@ import java.util.List;
 public class CommandWebSocketAutoConfiguration {
 
     @Configuration
-    @ConditionalOnMissingBean(WebSocketProcessorContainer.class)
+    @ConditionalOnMissingBean(CommandProcessorContainer.class)
     public static class WebSocketProcessorContainerConfiguration {
         @Autowired(required = false)
-        private List<WebSocketProcessor> webSocketProcessors;
+        private List<CommandProcessor> commandProcessors;
 
         @Bean(destroyMethod = "destroy")
-        public DefaultWebSocketProcessorContainer defaultWebSocketProcessorContainer() {
-            DefaultWebSocketProcessorContainer container = new DefaultWebSocketProcessorContainer();
-            if (webSocketProcessors != null) {
-                webSocketProcessors.forEach(container::install);
+        public DefaultCommandProcessorContainer defaultWebSocketProcessorContainer() {
+            DefaultCommandProcessorContainer container = new DefaultCommandProcessorContainer();
+            if (commandProcessors != null) {
+                commandProcessors.forEach(container::install);
             }
             return container;
         }
@@ -62,12 +62,12 @@ public class CommandWebSocketAutoConfiguration {
         private List<WebSocketSessionListener> webSocketSessionListeners;
 
         @Autowired
-        private WebSocketProcessorContainer webSocketProcessorContainer;
+        private CommandProcessorContainer commandProcessorContainer;
 
         @Override
         protected void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
             CommandWebSocketMessageDispatcher dispatcher = new CommandWebSocketMessageDispatcher();
-            dispatcher.setProcessorContainer(webSocketProcessorContainer);
+            dispatcher.setProcessorContainer(commandProcessorContainer);
             dispatcher.setAuthenticationContainer(authenticationContainer);
             dispatcher.setWebSocketSessionListeners(webSocketSessionListeners);
             registry.addHandler(dispatcher, "/sockjs")

+ 3 - 3
hsweb-message/hsweb-message-websocket/src/test/java/org/hswebframework/web/socket/TestProcessor.java

@@ -4,7 +4,7 @@ import org.hswebframework.web.message.MessageSubscribe;
 import org.hswebframework.web.message.Messager;
 import org.hswebframework.web.message.support.ObjectMessage;
 import org.hswebframework.web.socket.message.WebSocketMessage;
-import org.hswebframework.web.socket.processor.WebSocketProcessor;
+import org.hswebframework.web.socket.processor.CommandProcessor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.socket.TextMessage;
 import org.springframework.web.socket.WebSocketSession;
@@ -21,7 +21,7 @@ import static org.hswebframework.web.message.builder.StaticMessageSubjectBuilder
  *
  * @author zhouhao
  */
-public class TestProcessor implements WebSocketProcessor, WebSocketSessionListener {
+public class TestProcessor implements CommandProcessor, WebSocketSessionListener {
 
     @Autowired
     private Messager messager;
@@ -61,7 +61,7 @@ public class TestProcessor implements WebSocketProcessor, WebSocketSessionListen
     }
 
     @Override
-    public void execute(WebSocketCommand command) {
+    public void execute(CommandRequest command) {
         String type = String.valueOf(command.getParameters().get("type"));
         switch (type) {
             case "conn":

+ 1 - 2
hsweb-message/hsweb-message-websocket/src/test/java/org/hswebframework/web/socket/WebSocketClientTest.java

@@ -8,8 +8,7 @@ import org.springframework.web.socket.client.WebSocketClient;
 import org.springframework.web.socket.client.standard.StandardWebSocketClient;
 import org.springframework.web.socket.handler.AbstractWebSocketHandler;
 
-public class WebSocketClientTest {
-
+public class WebSocketClientTests {
     public static void main(String[] args) throws Exception {
         for (int i = 0; i < 10; i++) {
             WebSocketClient client = new StandardWebSocketClient();