feat: Update PacketOnlineSubscribe and PacketTyping to include privateKey and enhance logging

This commit is contained in:
k1ngsterr1
2026-01-12 04:14:28 +05:00
parent 8237c72c17
commit e04010d720
6 changed files with 126 additions and 35 deletions

View File

@@ -141,27 +141,54 @@ class PacketUserInfo : Packet() {
}
}
/**
* Online State enum
*/
enum class OnlineState(val value: Int) {
ONLINE(0),
OFFLINE(1);
companion object {
fun fromBoolean(isOnline: Boolean) = if (isOnline) ONLINE else OFFLINE
}
}
/**
* Public key with online state
*/
data class PublicKeyOnlineState(
val publicKey: String,
val state: OnlineState
)
/**
* Online State packet (ID: 0x05)
* Notify about user online status
* Формат как в React Native: массив {publicKey, state}
*/
class PacketOnlineState : Packet() {
var publicKey: String = ""
var online: Int = 0
var lastSeen: Long = 0
var publicKeysState: MutableList<PublicKeyOnlineState> = mutableListOf()
override fun getPacketId(): Int = 0x05
override fun receive(stream: Stream) {
publicKey = stream.readString()
online = stream.readInt8()
lastSeen = stream.readInt64()
val count = stream.readInt8()
publicKeysState.clear()
repeat(count) {
val publicKey = stream.readString()
val isOnline = stream.readBoolean()
publicKeysState.add(PublicKeyOnlineState(publicKey, OnlineState.fromBoolean(isOnline)))
}
}
override fun send(): Stream {
val stream = Stream()
stream.writeInt16(getPacketId())
stream.writeString(publicKey)
stream.writeInt8(publicKeysState.size)
publicKeysState.forEach { item ->
stream.writeString(item.publicKey)
stream.writeBoolean(item.state == OnlineState.ONLINE)
}
return stream
}
}
@@ -169,22 +196,37 @@ class PacketOnlineState : Packet() {
/**
* Online Subscribe packet (ID: 0x04)
* Subscribe to user online status updates
* Формат как в React Native: privateKey + array of publicKeys
*/
class PacketOnlineSubscribe : Packet() {
var publicKey: String = ""
var privateKey: String = ""
var publicKeys: MutableList<String> = mutableListOf()
override fun getPacketId(): Int = 0x04
override fun receive(stream: Stream) {
publicKey = stream.readString()
privateKey = stream.readString()
val keysCount = stream.readInt16()
publicKeys.clear()
repeat(keysCount) {
publicKeys.add(stream.readString())
}
}
override fun send(): Stream {
val stream = Stream()
stream.writeInt16(getPacketId())
stream.writeString(publicKey)
stream.writeString(privateKey)
stream.writeInt16(publicKeys.size)
publicKeys.forEach { key ->
stream.writeString(key)
}
return stream
}
fun addPublicKey(publicKey: String) {
publicKeys.add(publicKey)
}
}
// ============================================================================
@@ -347,14 +389,19 @@ class PacketDelivery : Packet() {
* Typing packet (ID: 0x0B)
* Уведомление "печатает..."
*/
/**
* Typing packet (ID: 0x0B)
* Порядок полей как в React Native: privateKey, fromPublicKey, toPublicKey
*/
class PacketTyping : Packet() {
var privateKey: String = ""
var fromPublicKey: String = ""
var toPublicKey: String = ""
var privateKey: String = ""
override fun getPacketId(): Int = 0x0B
override fun receive(stream: Stream) {
privateKey = stream.readString()
fromPublicKey = stream.readString()
toPublicKey = stream.readString()
}
@@ -362,9 +409,9 @@ class PacketTyping : Packet() {
override fun send(): Stream {
val stream = Stream()
stream.writeInt16(getPacketId())
stream.writeString(privateKey)
stream.writeString(fromPublicKey)
stream.writeString(toPublicKey)
stream.writeString(privateKey)
return stream
}
}

View File

@@ -301,12 +301,14 @@ class Protocol(
handshakeComplete = false
handshakeJob?.cancel()
heartbeatJob?.cancel()
webSocket = null // Обнуляем сокет чтобы connect() мог создать новый
// Автоматический reconnect (как в Архиве)
if (!isManuallyClosed) {
// Сбрасываем счетчик если до этого были подключены
// Логируем потерю соединения
if (previousState == ProtocolState.AUTHENTICATED || previousState == ProtocolState.CONNECTED) {
log("🔄 Connection lost, will reconnect...")
log("🔄 Connection lost from $previousState, will reconnect...")
reconnectAttempts = 0 // Сбрасываем счётчик при неожиданной потере
}
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {