diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetRoomUsecase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetRoomUsecase.kt index 0c64df28..ce2995ce 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetRoomUsecase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetRoomUsecase.kt @@ -32,10 +32,10 @@ class GetRoomUsecase( data class Request( val roomId: String, - val userIdentity: String + val userIdentity: String, ) interface Presenter { fun present(room: Room) } -} \ No newline at end of file +} diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt new file mode 100644 index 00000000..31ac09dc --- /dev/null +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt @@ -0,0 +1,7 @@ +package tw.waterballsa.gaas.exceptions.enums + +enum class SocketIOEventMessage(val eventName: String) { + CHAT_MESSAGE("CHAT_MESSAGE"), + CHATROOM_JOIN("CHATROOM_JOIN"), + CONNECT_EVENT("CONNECT_EVENT"), +} diff --git a/spring/pom.xml b/spring/pom.xml index 6cfb9da0..0ff1acf8 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -19,6 +19,7 @@ 2.14.2 1.6.15 17 + 2.0.3 @@ -94,6 +95,11 @@ org.springframework.boot spring-boot-starter-webflux + + com.corundumstudio.socketio + netty-socketio + ${netty-socketio.version} + @@ -166,4 +172,5 @@ + diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/LobbyPlatformApplication.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/LobbyPlatformApplication.kt index 4fa2f83a..c37a27b5 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/LobbyPlatformApplication.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/LobbyPlatformApplication.kt @@ -3,7 +3,6 @@ package tw.waterballsa.gaas.spring import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication - @SpringBootApplication(scanBasePackages = ["tw.waterballsa.gaas"]) class LobbyPlatformApplication diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/ServerCommandLineRunner.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/ServerCommandLineRunner.kt new file mode 100644 index 00000000..d29fd663 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/ServerCommandLineRunner.kt @@ -0,0 +1,30 @@ +package tw.waterballsa.gaas.spring.configs.socketio + +import com.corundumstudio.socketio.SocketIOServer +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.boot.CommandLineRunner +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty +import org.springframework.context.annotation.Configuration +import org.springframework.core.annotation.Order +import javax.annotation.PreDestroy + +@Configuration +@Order(1) +@ConditionalOnProperty(prefix = "socketio", name = ["active"], havingValue = "true") +class ServerCommandLineRunner(private val server: SocketIOServer) : CommandLineRunner { + + private val logger: Logger = LoggerFactory.getLogger(ServerCommandLineRunner::class.java) + + override fun run(vararg args: String?) { + server.start() + } + + @PreDestroy + fun stopServer() { + logger.info("Stopping the server...") + server.stop() + logger.info("Server stopped.") + + } +} \ No newline at end of file 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 new file mode 100644 index 00000000..74ac8736 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt @@ -0,0 +1,24 @@ +package tw.waterballsa.gaas.spring.configs.socketio + +import com.corundumstudio.socketio.SocketIOServer +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + + +@Configuration +@EnableConfigurationProperties(SocketIOProperties::class) +class SocketIOConfig (){ + + @Bean + fun socketIOServer(socketIOProperties: SocketIOProperties): SocketIOServer { + val configuration = com.corundumstudio.socketio.Configuration() + .apply { + with(socketIOProperties){ + hostname = socketHost + port = socketPort + } + } + return SocketIOServer(configuration) + } +} \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEvent.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEvent.kt new file mode 100644 index 00000000..3b25ad84 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEvent.kt @@ -0,0 +1,7 @@ +package tw.waterballsa.gaas.spring.configs.socketio + +enum class SocketIOEvent (val eventName: String){ + CHAT_MESSAGE("CHAT_MESSAGE"), + CHATROOM_JOIN("CHATROOM_JOIN"), + CONNECT_EVENT("CONNECT_EVENT"); +} \ 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 new file mode 100644 index 00000000..bfb39093 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt @@ -0,0 +1,35 @@ +package tw.waterballsa.gaas.spring.configs.socketio + +import com.corundumstudio.socketio.SocketIOClient +import com.corundumstudio.socketio.SocketIOServer +import com.nimbusds.jose.shaded.json.JSONObject +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component + +@Component +class SocketIOEventHandler(private val socketIOServer: SocketIOServer) { + + private val logger: Logger = LoggerFactory.getLogger(SocketIOEventHandler::class.java) + + init { + configureEventHandlers() + } + + private fun configureEventHandlers() { + socketIOServer.addEventListener(SocketIOEvent.CHAT_MESSAGE.eventName, JSONObject::class.java) { client: SocketIOClient, data: JSONObject, _ -> + // Handle the "chatMessage" event + logger.info(" Received message: $data from client: ${client.sessionId}") + // ECHO + client.sendEvent(SocketIOEvent.CHAT_MESSAGE.eventName, data) + } + + socketIOServer.addEventListener(SocketIOEvent.CHATROOM_JOIN.eventName, JSONObject::class.java) { + client: SocketIOClient, data: JSONObject, _ -> + // ECHO + logger.info(" Received message: $data from client: ${client.sessionId}") + + client.sendEvent(SocketIOEvent.CHATROOM_JOIN.eventName, data) + } + } +} \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOProperties.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOProperties.kt new file mode 100644 index 00000000..db2db5a9 --- /dev/null +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOProperties.kt @@ -0,0 +1,9 @@ +package tw.waterballsa.gaas.spring.configs.socketio + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties(prefix = "socketio") +data class SocketIOProperties ( + val socketHost: String = "127.0.0.1", + val socketPort: Int = 9001){ +} \ No newline at end of file diff --git a/spring/src/main/resources/application-dev.yaml b/spring/src/main/resources/application-dev.yaml index d3857369..e7340b87 100644 --- a/spring/src/main/resources/application-dev.yaml +++ b/spring/src/main/resources/application-dev.yaml @@ -41,3 +41,8 @@ logging: level: org.springframework.security: trace +socketio: + host: 127.0.0.1 + port: 9001 + active: false +