diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/events/SocketioEvent.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/events/SocketioEvent.kt deleted file mode 100644 index 3d1895ac..00000000 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/events/SocketioEvent.kt +++ /dev/null @@ -1,21 +0,0 @@ -package tw.waterballsa.gaas.events - -data class SocketioEvent( - var type: String = "", - var data: ChatData = ChatData(), -) : DomainEvent() { - constructor(type: String, userId: String, nickname: String, target: String) : this( - type, - ChatData(ChatUser(userId, nickname), target), - ) -} - -data class ChatData( - var user: ChatUser = ChatUser(), - var target: String = "", -) - -data class ChatUser( - var id: String = "", - var nickname: String = "", -) diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt index 36bf69b9..dd6ff4c4 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt @@ -1,14 +1,10 @@ package tw.waterballsa.gaas.spring.configs.socketio -import com.corundumstudio.socketio.AuthorizationListener import com.corundumstudio.socketio.SocketIOServer -import com.corundumstudio.socketio.listener.DefaultExceptionListener +import com.corundumstudio.socketio.annotation.SpringAnnotationScanner import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -import org.springframework.web.cors.CorsConfiguration -import org.springframework.web.cors.reactive.CorsWebFilter -import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource @Configuration @@ -25,9 +21,11 @@ class SocketIOConfig{ allowHeaders = "Authorization, CustomHeaderName" } } - - return SocketIOServer(configuration) } + @Bean + fun springAnnotationScanner(socketIOServer: SocketIOServer): SpringAnnotationScanner { + return SpringAnnotationScanner(socketIOServer) + } } \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt index f1c492fa..41271078 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt @@ -1,17 +1,20 @@ package tw.waterballsa.gaas.spring.configs.socketio +import com.corundumstudio.socketio.AckRequest import com.corundumstudio.socketio.SocketIOClient import com.corundumstudio.socketio.SocketIOServer -import io.netty.handler.codec.http.HttpHeaderNames +import com.corundumstudio.socketio.annotation.OnConnect +import com.corundumstudio.socketio.annotation.OnDisconnect +import com.corundumstudio.socketio.annotation.OnEvent import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.stereotype.Component import tw.waterballsa.gaas.application.eventbus.EventBus import tw.waterballsa.gaas.application.repositories.RoomRepository import tw.waterballsa.gaas.application.repositories.UserRepository -import tw.waterballsa.gaas.events.ChatData -import tw.waterballsa.gaas.events.SocketioEvent -import tw.waterballsa.gaas.events.enums.EventMessageType +import tw.waterballsa.gaas.spring.configs.socketio.event.SocketIOChatEvent +import tw.waterballsa.gaas.spring.configs.socketio.event.SocketIORoomEvent +import java.time.Instant @Component @@ -25,76 +28,41 @@ class SocketIOEventHandler( private val logger: Logger = LoggerFactory.getLogger(SocketIOEventHandler::class.java) - init { - configureEventHandlers() + @OnConnect + fun onConnect(client: SocketIOClient){ + val token = client.handshakeData.getSingleUrlParam("token") + logger.info("user connect, SessionId: {}, token: {}", client.sessionId, token) } + @OnDisconnect + fun onDisconnect(client: SocketIOClient){ + val token = client.handshakeData.getSingleUrlParam("token") + logger.info("user disconnect, SessionId: {}, token: {}", client.sessionId, token) + client.disconnect() + } - private final fun configureEventHandlers() { - - socketIOServer.addConnectListener { client -> - val token = client.handshakeData.httpHeaders.get(HttpHeaderNames.COOKIE) - val customHeader = client.handshakeData.getSingleUrlParam("Authorization") - - if (client != null ) { - logger.info("有新用戶連結 , SessionId: {}", client.sessionId) - val board = socketIOServer.broadcastOperations - logger.info("board clientId {}", board.clients) - } - } - - socketIOServer.addEventListener(EventMessageType.GAME_STARTED.eventName, SocketioEvent::class.java) - { client: SocketIOClient, socketioEvent: SocketioEvent, _ -> - - logger.info(" ... " ) - client.sendEvent(EventMessageType.GAME_STARTED.eventName, socketioEvent.data) - - } - - - - - - socketIOServer.addEventListener(SocketIOEventName.CHAT_MESSAGE.eventName, SocketioEvent::class.java) - { client: SocketIOClient, socketioEvent: SocketioEvent, _ -> - // Handle the "chatMessage" event - logger.info(" CHAT_MESSAGE Received message: $socketioEvent from client: ${client.sessionId}") - client.handshakeData.getSingleUrlParam("") - // ECHO - client.sendEvent(SocketIOEventName.CHAT_MESSAGE.eventName, socketioEvent.data) - - socketIOServer.broadcastOperations.sendEvent("test", socketioEvent.data) - - } - - socketIOServer.addEventListener(SocketIOEventName.JOIN_ROOM.eventName, ChatData::class.java) { - client: SocketIOClient, socketioEvent: ChatData, _ -> - - client.joinRoom(socketioEvent.target) - logger.info("Client joined room: ${socketioEvent.target}") - logger.info("id = " + socketioEvent.user.id + " nickname " + socketioEvent.user.nickname + " targetRoom " + socketioEvent.target) - logger.info(" room size is : ${client.getCurrentRoomSize(socketioEvent.target)}") - } - - - socketIOServer.addEventListener(SocketIOEventName.LEAVE_ROOM.eventName, ChatData::class.java) { - client: SocketIOClient, socketioEvent: ChatData, _ -> - // ECHO - logger.info(" LEAVE_ROOM Received message: ${socketioEvent.target} from client: ${client.sessionId}") - client.leaveRoom(socketioEvent.target) - } - - - socketIOServer.addEventListener(SocketIOEventName.DISCONNECT.eventName, SocketioEvent::class.java) { - client: SocketIOClient, _: SocketioEvent, _ -> - - client.disconnect() - logger.info(" client is leaven room with key disconnect") - } + @OnEvent(value = SocketIOEventName.JOIN_ROOM) + fun onJoinRoom(client: SocketIOClient, event: SocketIORoomEvent, ackRequest: AckRequest){ + client.joinRoom(event.target) + logger.info("Client joined room: ${event.target}") + logger.info("id = " + event.user.id + " nickname " + event.user.nickname + " targetRoom " + event.target) + logger.info(" room size is : ${client.getCurrentRoomSize(event.target)}") + } - socketIOServer.addDisconnectListener { + @OnEvent(value = SocketIOEventName.LEAVE_ROOM) + fun onLeaveRoom(client: SocketIOClient, event: SocketIORoomEvent, ackRequest: AckRequest){ + logger.info(" LEAVE_ROOM Received message: ${event.target} from client: ${client.sessionId}") + client.leaveRoom(event.target) + } - logger.info("Server disconnected on the server side") + @OnEvent(value = SocketIOEventName.CHAT_MESSAGE) + fun onChatMessage(client: SocketIOClient, event: SocketIOChatEvent, ackRequest: AckRequest){ + event.timestamp = Instant.now().toString() + val room = if(event.isLobby()){ + socketIOServer.broadcastOperations + }else{ + socketIOServer.getRoomOperations(event.target) } + room.sendEvent(SocketIOEventName.CHAT_MESSAGE, event) } } diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventName.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventName.kt index 29d7a030..1518d120 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventName.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventName.kt @@ -1,11 +1,8 @@ package tw.waterballsa.gaas.spring.configs.socketio -enum class SocketIOEventName (val eventName: String){ - CHAT_MESSAGE("CHAT_MESSAGE"), - CHATROOM_JOIN("CHATROOM_JOIN"), - JOIN_ROOM("JOIN_ROOM"), - LEAVE_ROOM("LEAVE_ROOM"), - CONNECT_EVENT("CONNECT_EVENT"), - DISCONNECT("DISCONNECT"); +object SocketIOEventName{ + const val CHAT_MESSAGE = "CHAT_MESSAGE" + const val JOIN_ROOM = "JOIN_ROOM" + const val LEAVE_ROOM = "LEAVE_ROOM" } diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOChatEvent.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOChatEvent.kt new file mode 100644 index 00000000..0b3aab55 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOChatEvent.kt @@ -0,0 +1,11 @@ +package tw.waterballsa.gaas.spring.configs.socketio.event + + +data class SocketIOChatEvent( + val from: SocketIOUser = SocketIOUser("", ""), + val content: String = "", + val target: String = "", + var timestamp: String? = null, +){ + fun isLobby() = target == "LOBBY" +} \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIORoomEvent.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIORoomEvent.kt new file mode 100644 index 00000000..fb8eb6e6 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIORoomEvent.kt @@ -0,0 +1,7 @@ +package tw.waterballsa.gaas.spring.configs.socketio.event + +data class SocketIORoomEvent( + val target: String = "", + val user: SocketIOUser = SocketIOUser("", ""), +) + diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOUser.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOUser.kt new file mode 100644 index 00000000..4d4ca034 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/event/SocketIOUser.kt @@ -0,0 +1,6 @@ +package tw.waterballsa.gaas.spring.configs.socketio.event + +data class SocketIOUser( + val id: String = "", + val nickname: String = "", +) diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/SocketioViewModel.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/SocketioViewModel.kt deleted file mode 100644 index b2dfbd45..00000000 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/SocketioViewModel.kt +++ /dev/null @@ -1,12 +0,0 @@ -package tw.waterballsa.gaas.spring.controllers.viewmodel - -import tw.waterballsa.gaas.events.ChatData -import tw.waterballsa.gaas.events.SocketioEvent - -data class SocketioViewModel( - - val datas : List -){ - data class SocketioEvent(val type: String, val data: ChatData) - -} \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/WebSocketEventBus.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/WebSocketEventBus.kt index 13fb0171..2a7f93f5 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/WebSocketEventBus.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/WebSocketEventBus.kt @@ -7,7 +7,6 @@ import org.springframework.stereotype.Component import tw.waterballsa.gaas.application.eventbus.EventBus import tw.waterballsa.gaas.events.DomainEvent import tw.waterballsa.gaas.events.RoomEvent -import tw.waterballsa.gaas.spring.controllers.viewmodel.SocketioViewModel import kotlin.reflect.safeCast @Component @@ -17,7 +16,6 @@ class WebSocketEventBus( private val logger: Logger = LoggerFactory.getLogger(WebSocketEventBus::class.java) - lateinit var viewModel: SocketioViewModel override fun broadcast(events: Collection) { events.asSequence() .mapNotNull { RoomEvent::class.safeCast(it) }