-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 更新socket io * 設定socket io 啟動 server 就會啟動 * 修正語法 * 改成socket io * 優化語法 --------- Co-authored-by: wiromi <>
- Loading branch information
1 parent
ea463af
commit 87ad512
Showing
10 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
domain/src/main/kotlin/tw/waterballsa/gaas/exceptions/enums/SocketIOEventMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ng/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/ServerCommandLineRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
35 changes: 35 additions & 0 deletions
35
spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring/src/main/kotlin/tw/waterballsa/gaas/spring/configs/socketio/SocketIOProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,8 @@ logging: | |
level: | ||
org.springframework.security: trace | ||
|
||
socketio: | ||
host: 127.0.0.1 | ||
port: 9001 | ||
active: false | ||
|