Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions websocket_fdddf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# websocket
IDE前后端使用websocket通信,后端主动可以向前端发送消息

## 逻辑详情

### registerMessageHandler

注册消息处理逻辑函数。

入参: Function<CommonRequestMessage, CommonResponseMessage>
出参: Boolean 无意义

### sendMessage

向订阅的/topic/common发送消息,但不调用上述注册的逻辑函数

入参: CommonRequestMessage
出参: Boolean 无意义


CommonRequestMessage结构:
- content: String 消息内容
- userId: Long 发送者id

CommonReplyMessage结构
- content: String 消息内容
- userId: Long 发送者id

## 使用步骤说明

1. 应用引用依赖库
2. 无需配置应用配置参数
3. 逻辑调用示例截图

![](Snipaste_2024-10-20_16-45-59.jpg)


## 应用演示链接

[使用了本依赖库的制品应用链接]
https://dev-websocket-qa.app.codewave.163.com/dashboard/testwebsocket
Binary file added websocket_fdddf/Snipaste_2024-10-20_16-45-59.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml"
Binary file not shown.
74 changes: 74 additions & 0 deletions websocket_fdddf/jar/nasl-metadata-maven-plugin-1.3.0/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.3.0</version>
<packaging>maven-plugin</packaging>

<name>Nasl Metadata Maven Plugin</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-manager</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>3.3.9</version>
</dependency>

<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<goalPrefix>nasl-metadata-maven-plugin</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
</plugins>
</build>
</project>
78 changes: 78 additions & 0 deletions websocket_fdddf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一-->
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>fdddf</groupId>
<artifactId>websocket</artifactId>
<name>websocket</name>
<description>websocket依赖库,支持STOMP协议</description>
<version>1.0.6</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nasl.ide.version>3.3</nasl.ide.version>
</properties>
<dependencies>
<!--本案例是本地系统引入nasl-metadata-collector-0.8.0.jar的方式。
若把nasl-metadata-collector-0.8.0.jar安装到自己的maven仓库,
注意修改artifactId和groupId的情况下,不要使用<scope>system</scope>,会在发布时造成依赖中断。
不修改artifactId和groupId的情况下,nasl-metadata-maven-plugin会做特殊处理-->
<dependency>
<artifactId>nasl-metadata-collector</artifactId>
<groupId>com.netease.lowcode</groupId>
<version>0.8.0</version>
<optional>true</optional>
<scope>system</scope>
<systemPath>${project.basedir}/jar/nasl-metadata-collector-0.8.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!--制品应用使用Springboot框架,父应用引用了的包,为了防止版本冲突,scope可设置为provided-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<jarWithDependencies>false</jarWithDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>archive</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fdddf.websocket;

/**
* 依赖库自动扫描类
* @author system
*/
public class LibraryAutoScan {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fdddf.websocket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MessagingStompWebsocketApplication {
public static void main(String[] args) {
SpringApplication.run(MessagingStompWebsocketApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fdddf.websocket;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* 加入spring环境配置(在spring.factories中指定)
*/
@Configuration
@ComponentScan(basePackageClasses = LibraryAutoScan.class)
public class WebsocketBasicSpringEnvironmentConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fdddf.websocket;

public class WebsocketException extends RuntimeException {
private static final long serialVersionUID = 1L;
public WebsocketException(String message) {
super(message);
}
public WebsocketException(String message, Throwable cause) {
super(message, cause);
}
public WebsocketException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fdddf.websocket.config;

import com.netease.lowcode.core.EnvironmentType;
import com.netease.lowcode.core.annotation.Environment;
import com.netease.lowcode.core.annotation.NaslConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOrigins("*");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fdddf.websocket.controller;

import com.fdddf.websocket.service.*;
import com.netease.lowcode.core.annotation.NaslLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.function.Function;

@Component
@Controller
public class MessageController {
@MessageMapping("/common")
@SendTo("/topic/common")
public CommonReplyMessage reply(CommonRequestMessage message) {
Function<CommonRequestMessage, CommonReplyMessage> handler = Register.getMessageHandler();
if (handler == null) {
return new CommonReplyMessage("No handler found to process message: "+ message.content, message.userId);
}
return handler.apply(message);
}

@Autowired
private SimpMessagingTemplate template;

/**
* 向/topic/common发送消息 [测试用]
* @param message CommonRequestMessage
* @return CommonRequestMessage
*/
@ResponseBody
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
public CommonRequestMessage testSendMessage(@RequestBody CommonRequestMessage message)
{
this.template.convertAndSend("/topic/common", message);
return message;
}

/**
* 向/topic/common发送消息
* @param body CommonRequestMessage
* @return Boolean
*/
@NaslLogic
public Boolean sendMessage(CommonRequestMessage body)
{
this.template.convertAndSend("/topic/common", body);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.fdddf.websocket.service;

import com.netease.lowcode.core.annotation.NaslStructure;

@NaslStructure
public class CommonReplyMessage {
/**
* 消息内容
*/
public String content;

/**
* 发送者id
*/
public Long userId;

public CommonReplyMessage() {}

public CommonReplyMessage(String content, Long userId)
{
this.content = content;
this.userId = userId;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.fdddf.websocket.service;

import com.netease.lowcode.core.annotation.NaslStructure;

@NaslStructure
public class CommonRequestMessage {
/**
* 消息内容
*/
public String content;

/**
* 发送者id
*/
public Long userId;

public CommonRequestMessage() {}

public CommonRequestMessage(String message, long userId) {
this.content = message;
this.userId = userId;
}
}
Loading