Page History
...
- web.reactive.socket 하위항목 사용
Warning |
---|
주의 org.springframework.boot:spring-boot-starter-web , org.springframework.boot:spring-boot-starter-webflux 둘다 포함시켜도 빌드는 잘작동되지만 런타임은 MVC우선으로 작동됩니다. 프로그래밍 방식도 다르기때문에 전환되는형태가 아니니 프로젝트 구성시 둘중 하나만 채택해 MVC/Reactive 모드로 갈지 초기에 선택합니다. |
SocketHandler 구현
MVC
Code Block | ||
---|---|---|
| ||
package com.example.kotlinbootlabs.ws import com.example.kotlinbootlabs.ws.handler.auth.EventTextMessage import com.example.kotlinbootlabs.ws.handler.auth.MessageFrom import com.example.kotlinbootlabs.ws.handler.auth.MessageType import com.example.kotlinbootlabs.ws.handler.auth.sendEventTextMessage import org.springframework.web.socket.TextMessage import org.springframework.web.socket.WebSocketSession import org.springframework.web.socket.handler.TextWebSocketHandler import org.springframework.stereotype.Component @Component class MyWebSocketHandler(private val sessionManager: WebSocketSessionManager) : TextWebSocketHandler() { override fun afterConnectionEstablished(session: WebSocketSession) { sessionManager.addSession(session) } override fun afterConnectionClosed(session: WebSocketSession, status: org.springframework.web.socket.CloseStatus) { sessionManager.removeSession(session) } override fun handleTextMessage(session: WebSocketSession, message: TextMessage) { val payload = message.payload when { payload.startsWith("subscribe:") -> { val topic = payload.substringAfter("subscribe:") sessionManager.subscribeToTopic(session.id, topic) } payload.startsWith("unsubscribe:") -> { val topic = payload.substringAfter("unsubscribe:") sessionManager.unsubscribeFromTopic(session.id, topic) } else -> { sendEventTextMessage(session, EventTextMessage( type = MessageType.CHAT, message = "Echo: $payload", from = MessageFrom.SYSTEM, id = null, jsondata = null, )) } } } } |
...