Skip to content

Commit

Permalink
Feature/socketio testing (#142)
Browse files Browse the repository at this point in the history
* 更新socket io

* 設定socket io 啟動 server 就會啟動

* 修正語法

* 改成socket io

* 優化語法

---------

Co-authored-by: wiromi <>
  • Loading branch information
wiroger9595 authored Aug 12, 2023
1 parent ea463af commit 87ad512
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class GetRoomUsecase(

data class Request(
val roomId: String,
val userIdentity: String
val userIdentity: String,
)

interface Presenter {
fun present(room: Room)
}
}
}
Original file line number Diff line number Diff line change
@@ -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"),
}
7 changes: 7 additions & 0 deletions spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<jackson-module-kotlin.version>2.14.2</jackson-module-kotlin.version>
<spring-openapi.version>1.6.15</spring-openapi.version>
<java.version>17</java.version>
<netty-socketio.version>2.0.3</netty-socketio.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -94,6 +95,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>${netty-socketio.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -166,4 +172,5 @@
</properties>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.")

}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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){
}
5 changes: 5 additions & 0 deletions spring/src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ logging:
level:
org.springframework.security: trace

socketio:
host: 127.0.0.1
port: 9001
active: false

0 comments on commit 87ad512

Please sign in to comment.