mirror of
https://github.com/SOR2171/Springboot4-Vue3_Demo.git
synced 2026-10-23 11:31:10 +08:00
refactor: simplify WebSocketConfig with Annotation
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyShadowingBuiltinsInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredNames">
|
||||
<list>
|
||||
<option value="id" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@@ -1,29 +1,24 @@
|
||||
package com.github.sor2171.backend.config
|
||||
|
||||
import com.github.sor2171.backend.handler.TestHandler
|
||||
import com.github.sor2171.backend.handler.v1.ExampleHandler
|
||||
import com.github.sor2171.backend.handler.WsHandler
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.reactive.HandlerMapping
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
|
||||
import org.springframework.web.reactive.socket.WebSocketHandler
|
||||
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
|
||||
@Configuration
|
||||
class WebSocketConfig(
|
||||
private val exampleHandler: ExampleHandler,
|
||||
private val testHandler: TestHandler
|
||||
private val handlers: List<WebSocketHandler>
|
||||
) {
|
||||
|
||||
@Bean
|
||||
fun handlerMapping(): HandlerMapping {
|
||||
val map = mapOf(
|
||||
"/ws/v1/example" to exampleHandler,
|
||||
"/ws/test" to testHandler
|
||||
)
|
||||
val mapping = SimpleUrlHandlerMapping()
|
||||
mapping.urlMap = map
|
||||
mapping.order = 1
|
||||
return mapping
|
||||
val map = handlers.associateBy {
|
||||
it::class.findAnnotation<WsHandler>()!!.path
|
||||
}
|
||||
return SimpleUrlHandlerMapping(map, 1)
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -6,9 +6,9 @@ import org.springframework.web.reactive.socket.WebSocketSession
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Component
|
||||
@WsHandler("/ws/test")
|
||||
class TestHandler : WebSocketHandler {
|
||||
override fun handle(session: WebSocketSession): Mono<Void> {
|
||||
// ws://localhost:8080/ws/test
|
||||
val output = session.receive()
|
||||
.map { msg ->
|
||||
val userText = msg.payloadAsText
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.github.sor2171.backend.handler
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class WsHandler(val path: String)
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.github.sor2171.backend.handler.v1
|
||||
|
||||
import com.github.sor2171.backend.handler.WsHandler
|
||||
import com.github.sor2171.backend.service.WsTokenService
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.reactive.socket.WebSocketHandler
|
||||
@@ -7,11 +8,11 @@ import org.springframework.web.reactive.socket.WebSocketSession
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Component
|
||||
@WsHandler("/ws/v1/example")
|
||||
class ExampleHandler(
|
||||
private val wsTokenService: WsTokenService,
|
||||
) : WebSocketHandler {
|
||||
override fun handle(session: WebSocketSession): Mono<Void> {
|
||||
// ws://localhost:8080/ws/v1/example?token=
|
||||
val output = session.receive()
|
||||
.map { msg ->
|
||||
val userText = msg.payloadAsText
|
||||
|
||||
Reference in New Issue
Block a user