This commit is contained in:
2026-03-13 10:41:10 +09:00
parent eb2dc0efd0
commit 6d340b7dc0
8 changed files with 307 additions and 77 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ import model.TokenRequest
import model.TokenResponse
import java.time.LocalDateTime
class KisAuthService {
object KisAuthService {
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
+14 -4
View File
@@ -14,7 +14,7 @@ import model.RealTimeTrade
import util.AesCrypto
import java.util.concurrent.atomic.AtomicBoolean
class KisWebSocketManager {
object KisWebSocketManager {
private val client = HttpClient {
install(WebSockets) {
pingInterval = 20_000 // 20초마다 표준 웹소켓 핑 전송 (서버-클라이언트 연결 유지 도움)
@@ -23,7 +23,7 @@ class KisWebSocketManager {
private var session: DefaultClientWebSocketSession? = null
private val isConnected = AtomicBoolean(false)
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private var connectJob: Job? = null
// 콜백 리스너
var onPriceUpdate: ((RealTimeTrade) -> Unit)? = null
@@ -32,8 +32,8 @@ class KisWebSocketManager {
suspend fun connect() {
if (isConnected.get()) return
val url = if (KisSession.config.isSimulation) "ops.koreainvestment.com:31000" else "ops.koreainvestment.com:21000"
scope.launch {
connectJob?.cancelAndJoin()
connectJob = scope.launch {
while (isActive) { // 재연결을 위한 루프 추가
try {
client.webSocket(method = HttpMethod.Get, host = url.split(":")[0], port = url.split(":")[1].toInt(), path = "/last_price") {
@@ -192,4 +192,14 @@ class KisWebSocketManager {
activeSubscriptions.addAll(requiredCodes)
}
suspend fun disconnect() {
println("🔌 웹소켓 연결 종료 중...")
isConnected.set(false)
connectJob?.cancelAndJoin() // 루프 자체를 중단
session?.close()
session = null
connectJob = null
println("🛑 웹소켓 완전 종료")
}
}