diff --git a/pom.xml b/pom.xml index 40c149540355a7e235612eefb29a038963a2c6fc..12d7c86785e15f8d77923f1edcaee3ef1bd2f12b 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ <module>tutor-system</module> <module>tutor-support</module> <module>tutor-framework</module> + <module>tutor-chatting</module> </modules> <packaging>pom</packaging> @@ -47,6 +48,7 @@ <properties> <java.version>11</java.version> + <kotlin.version>1.6.10</kotlin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <druid.version>1.2.5</druid.version> @@ -63,6 +65,7 @@ <spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-boot.version>2.3.12.RELEASE</spring-boot.version> <spring-boot.admin.version>2.3.1</spring-boot.admin.version> + <tio.websocket.version>1.0.0-RELEASE</tio.websocket.version> <dynamic-datasource.version>3.4.1</dynamic-datasource.version> <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version> <jrebel-maven-plugin.version>1.1.10</jrebel-maven-plugin.version> @@ -173,6 +176,22 @@ <artifactId>tutor-framework</artifactId> <version>${tutor.version}</version> </dependency> + <dependency> + <groupId>com.github.fanpan26</groupId> + <artifactId>tio-websocket-spring-boot-starter</artifactId> + <version>${tio.websocket.version}</version> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-stdlib-jdk8</artifactId> + <version>${kotlin.version}</version> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-test</artifactId> + <version>${kotlin.version}</version> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> @@ -228,6 +247,30 @@ <configFile>${basedir}/src/main/resources/smart-doc.json</configFile> </configuration> </plugin> + <plugin> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-maven-plugin</artifactId> + <version>${kotlin.version}</version> + <executions> + <execution> + <id>compile</id> + <phase>compile</phase> + <goals> + <goal>compile</goal> + </goals> + </execution> + <execution> + <id>test-compile</id> + <phase>test-compile</phase> + <goals> + <goal>test-compile</goal> + </goals> + </execution> + </executions> + <configuration> + <jvmTarget>${java.version}</jvmTarget> + </configuration> + </plugin> </plugins> </pluginManagement> </build> @@ -257,4 +300,4 @@ </pluginRepository> </pluginRepositories> -</project> \ No newline at end of file +</project> diff --git a/tutor-chatting/pom.xml b/tutor-chatting/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..260d33c65dfaecb0fb8c3bcb53648dfb579d0f44 --- /dev/null +++ b/tutor-chatting/pom.xml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>tutor</artifactId> + <groupId>cn.edu.tjnu</groupId> + <version>2.0-RC</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>tutor-chatting</artifactId> + + <properties> + <kotlin.version>1.6.10</kotlin.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>com.github.fanpan26</groupId> + <artifactId>tio-websocket-spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-stdlib-jdk8</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>cn.edu.tjnu</groupId> + <artifactId>tutor-common</artifactId> + </dependency> + </dependencies> + + <build> + <sourceDirectory>src/main/kotlin</sourceDirectory> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-maven-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/ChattingServerApplication.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/ChattingServerApplication.kt new file mode 100644 index 0000000000000000000000000000000000000000..d5839fb8124613a8ef919906d0b4a04e79602a00 --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/ChattingServerApplication.kt @@ -0,0 +1,19 @@ +package cn.edu.tjnu.tutor.chatting + +import com.github.fanpan26.EnableTioWebSocketServer +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + + +@SpringBootApplication +@EnableTioWebSocketServer +open class ChattingServerApplication + +/** + * 聊天服务器入口 + * @author In_Chh + * @since 3.0 + */ +fun main(args: Array<String>) { + runApplication<ChattingServerApplication>(*args) +} diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/constant/chatting_server_constant.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/constant/chatting_server_constant.kt new file mode 100644 index 0000000000000000000000000000000000000000..e0203283e6f028d0334bd72deffb6f418f7fb3a1 --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/constant/chatting_server_constant.kt @@ -0,0 +1,11 @@ +@file:JvmName("ChattingServerConstantKt") + +package cn.edu.tjnu.tutor.chatting.constant + +/** + * 本模块用到的常量 + * @author In_Chh + * @since 3.0 + */ + +const val DEFAULT_GROUP_NAME = "default" diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/Message.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/Message.kt new file mode 100644 index 0000000000000000000000000000000000000000..9a9f1c70ae819051ba6beefb26f0015d9ab643b9 --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/Message.kt @@ -0,0 +1,16 @@ +package cn.edu.tjnu.tutor.chatting.entity + +import cn.hutool.core.date.DateTime + +/** + * 用户发送的消息实体类 + * @author In_Chh + * @since 3.0 + */ +data class Message( + val from: String, + val sendTo: String, + val messageCommand: MessageCommand, + val sendTime: DateTime = DateTime.now(), +) + diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/MessageCommand.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/MessageCommand.kt new file mode 100644 index 0000000000000000000000000000000000000000..a2b90bb69b88355f7afb0a285b55778577bae21e --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/entity/MessageCommand.kt @@ -0,0 +1,27 @@ +package cn.edu.tjnu.tutor.chatting.entity + +/** + * 指令枚举类 + */ +enum class MessageCommand(val value: Int) { + /** + * 发送群聊消息 + */ + SEND_GROUP_MESSAGE(0), + + /** + * 发送私聊消息 + */ + SEND_PRIVATE_MESSAGE(1), + + /** + * 加入群组 + */ + JOIN_GROUP(2), + + /** + * 退出群组 + */ + LEAVE_GROUP(3), + ; +} diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/handler/WsMsgHandler.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/handler/WsMsgHandler.kt new file mode 100644 index 0000000000000000000000000000000000000000..fd53b12fc1199b7a7f24a7d8245c66110e1c623a --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/handler/WsMsgHandler.kt @@ -0,0 +1,55 @@ +package cn.edu.tjnu.tutor.chatting.handler + +import cn.edu.tjnu.tutor.chatting.utils.getUserInfo +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component +import org.tio.core.ChannelContext +import org.tio.core.Tio +import org.tio.http.common.HttpRequest +import org.tio.http.common.HttpResponse +import org.tio.websocket.common.WsRequest +import org.tio.websocket.common.WsResponse +import org.tio.websocket.server.handler.IWsMsgHandler + +/** + * Websocket消息主处理器类,根据业务逻辑实现ws连接的生命周期钩子函数 + * @author In_Chh + * @since 3.0 + */ +@Component +class WsMsgHandler : IWsMsgHandler { + private val logger: Logger = LoggerFactory.getLogger(WsMsgHandler::class.java) + override fun handshake( + httpRequest: HttpRequest, + httpResponse: HttpResponse, + channelContext: ChannelContext, + ): HttpResponse { + logger.info("正在与{}握手", channelContext.clientNode.ip) + return httpResponse + } + + override fun onAfterHandshaked( + httpRequest: HttpRequest, + httpResponse: HttpResponse, + channelContext: ChannelContext, + ) { + logger.info("握手成功") + } + + override fun onBytes(wsRequest: WsRequest?, bytes: ByteArray?, channelContext: ChannelContext?): Any? { + return null + } + + override fun onClose(wsRequest: WsRequest?, bytes: ByteArray?, channelContext: ChannelContext?): Any? { + return null + } + + override fun onText(wsRequest: WsRequest, text: String, channelContext: ChannelContext): Any? { + val response = WsResponse.fromText(text, WsResponse.CHARSET_NAME) + Tio.sendToGroup(channelContext.tioConfig, "default", response) + return null + } + + private fun checkPermission(): Boolean = getUserInfo() != null +} diff --git a/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/utils/utils.kt b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/utils/utils.kt new file mode 100644 index 0000000000000000000000000000000000000000..5a5f0c3922b755f3b446c7a67cfc20c3f870c08f --- /dev/null +++ b/tutor-chatting/src/main/kotlin/cn/edu/tjnu/tutor/chatting/utils/utils.kt @@ -0,0 +1,12 @@ +package cn.edu.tjnu.tutor.chatting.utils + +import cn.edu.tjnu.tutor.common.core.domain.model.LoginUser + + +/** + * 获取用户信息 + * @return 用户实体对象 + */ +fun getUserInfo(): LoginUser? = TODO("从主服务器获取登录用户信息,待其他模块完成") + +