This commit is contained in:
2026-02-12 15:31:34 +09:00
parent da4ab01d5f
commit ad5e7e0ccb
8 changed files with 189 additions and 111 deletions
+20 -18
View File
@@ -10,6 +10,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import model.KisSession
import model.RealTimeTrade
import util.AesCrypto
import java.util.concurrent.atomic.AtomicBoolean
@@ -24,7 +25,8 @@ class KisWebSocketManager {
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
// 콜백 리스너
var onPriceUpdate: ((String, Double) -> Unit)? = null
var onPriceUpdate: ((RealTimeTrade) -> Unit)? = null
var onExecutionReceived: ((String, String, String, String, Boolean) -> Unit)? = null
suspend fun connect() {
@@ -70,8 +72,9 @@ class KisWebSocketManager {
}
private val _currentPrice = mutableStateOf("0")
val currentPrice = _currentPrice
// private val _currentPrice = mutableStateOf("0")
private val currentPrice = androidx.compose.runtime.mutableStateMapOf<String, String>()
// val currentPrice = _currentPrice
val tradeLogs = androidx.compose.runtime.mutableStateListOf<model.RealTimeTrade>()
@@ -111,20 +114,19 @@ class KisWebSocketManager {
if (trId == "H0STCNT0") {
val dataRows = parts[3].split("^")
val price = dataRows[2]
_currentPrice.value = price // 상태 업데이트
onPriceUpdate?.invoke(dataRows[0], price.toDoubleOrNull() ?: 0.0)
currentPrice[dataRows[0]] = price // 상태 업데이트
onPriceUpdate?.invoke(RealTimeTrade(
code = dataRows[0],
time = dataRows[1],
price = price,
change = dataRows[4],
volume = dataRows[12],
type = model.TradeType.NEUTRAL
))
// 로그 추가 (예시)
tradeLogs.add(
0, model.RealTimeTrade(
time = dataRows[1],
price = price,
change = dataRows[4],
volume = dataRows[12],
type = model.TradeType.NEUTRAL
)
)
if (tradeLogs.size > 50) tradeLogs.removeLast()
// if (tradeLogs.isNotEmpty() && tradeLogs.last().code)
}
}
@@ -133,7 +135,7 @@ class KisWebSocketManager {
// AES 복호화 실행
val decryptedData = AesCrypto.decrypt(parts[3], aesKey, aesIv)
val dataRows = decryptedData.split("^")
println("🔔 복호화된 체결 통보: ${if (dataRows[4] == "01") {"매도"} else {"매수"}} ${dataRows[8]} ${dataRows[9]}${dataRows[13]} 체결")
println("🔔 복호화된 체결 통보: ${if (dataRows[4] == "01") {"매도"} else {"매수"}} ${dataRows[8]} ${dataRows[9]}${if(dataRows[13] == "01"){"체결"}else{"접수"} }")
// UI 콜백 호출 (종목코드, 체결량, 체결가, 주문번호, 체결여부)
onExecutionReceived?.invoke(
@@ -150,13 +152,13 @@ class KisWebSocketManager {
fun clearData() {
tradeLogs.clear()
_currentPrice.value = "0"
currentPrice.clear()
}
suspend fun subscribeStock(code: String, isSubscribe: Boolean = true) {
val trType = if (isSubscribe) "1" else "2"
sendRequest(trType, "H0STCNT0", code)
if (isSubscribe) println("📡 구독 등록: $code") else println("📴 구독 해제: $code")
// if (isSubscribe) println("📡 구독 등록: $code") else println("📴 구독 해제: $code")
}
private suspend fun sendRequest(trType: String, trId: String, trKey: String) {