diff --git a/README.md b/README.md index 4a8c19e..7b2e4f5 100644 --- a/README.md +++ b/README.md @@ -448,7 +448,7 @@ onCommandPacket = { _, jsonObject -> 更多 `Command` 数据包的数据结构详见本项目的 [/record/直播弹幕](record/直播弹幕) 文件夹. -注意, `start()` 方法会 suspend 当前协程直到连接关闭, 如果当前协程上下文还需要执行更多逻辑则如下所示 +注意, `start()` 方法会 suspend 当前协程直到连接关闭, 如果当前协程还需要执行更多逻辑则如下所示 ```kotlin val liveClient = bilibiliClient.liveClient(args) @@ -468,5 +468,9 @@ onClose = { liveClient, closeReason -> } ``` +如果网络不可达, 那么 `start()` 方法会抛出异常, 且不会触发 `onConnect` 以及 `onClose` 回调. + +如果数据包被中间人修改, 那么可能不会触发 `onConnect` 回调, 但是会触发 `onClose`. + # License GPL V3 diff --git a/src/main/kotlin/com/hiczp/bilibili/api/CipherExtension.kt b/src/main/kotlin/com/hiczp/bilibili/api/CipherExtension.kt index 7407dfc..c81bb55 100644 --- a/src/main/kotlin/com/hiczp/bilibili/api/CipherExtension.kt +++ b/src/main/kotlin/com/hiczp/bilibili/api/CipherExtension.kt @@ -11,7 +11,7 @@ fun String.md5() = md5Instance.digest(toByteArray()).forEach { val value = it.toInt() and 0xFF val high = value / 16 - val low = value - high * 16 + val low = value % 16 append(if (high <= 9) '0' + high else 'a' - 10 + high) append(if (low <= 9) '0' + low else 'a' - 10 + low) } @@ -20,4 +20,4 @@ fun String.md5() = /** * 签名算法为 "$排序后的参数字符串$appSecret".md5() */ -fun calculateSign(sortedQuery: String, appSecret: String) = (sortedQuery + appSecret).md5() +internal fun calculateSign(sortedQuery: String, appSecret: String) = (sortedQuery + appSecret).md5() diff --git a/src/main/kotlin/com/hiczp/bilibili/api/live/websocket/Packet.kt b/src/main/kotlin/com/hiczp/bilibili/api/live/websocket/Packet.kt index e31cdf1..846e922 100644 --- a/src/main/kotlin/com/hiczp/bilibili/api/live/websocket/Packet.kt +++ b/src/main/kotlin/com/hiczp/bilibili/api/live/websocket/Packet.kt @@ -10,18 +10,18 @@ import java.nio.ByteBuffer * 数据包模型 * 由于 Android APP 并未全线换成 wss, 以下用的是移动版网页的协议 * 数据包头部结构 00 00 00 65 00 10 00 01 00 00 00 07 00 00 00 01 - * |数据包总长度| |头长| |ver| |数据包类型 | | single | + * |数据包总长度| |头长| |tag| |数据包类型 | | tag | * - * @param protocolVersion 协议版本 + * @param tagShort 一种 tag, 如果是非 command 数据包则为 1, 否则为 0, short 类型 * @param packetType 数据包类型 - * @param single 如果一个 Message 只有一个数据包则为 1, 否则为 0 + * @param tag 同 tagShort, 但是为 int 类型 * @param content 正文内容 */ @Suppress("MemberVisibilityCanBePrivate") class Packet( - val protocolVersion: Short = 1, + val tagShort: Short = 1, val packetType: PacketType, - val single: Int = 1, + val tag: Int = 1, val content: ByteBuffer ) { val totalLength @@ -33,9 +33,9 @@ class Packet( ByteBuffer.allocate(totalLength) .putInt(totalLength) .putShort(headerLength) - .putShort(protocolVersion) + .putShort(tagShort) .putInt(packetType.value) - .putInt(single) + .putInt(tag) .put(content).apply { flip() }!! @@ -59,14 +59,14 @@ internal fun Frame.toPackets(): List { val startPosition = buffer.position() val totalLength = buffer.int buffer.position(buffer.position() + 2) //skip headerLength - val protocolVersion = buffer.short + val tagShort = buffer.short val packetType = PacketType.getByValue(buffer.int) - val sequence = buffer.int + val tag = buffer.int buffer.limit(startPosition + totalLength) val content = buffer.slice() buffer.position(buffer.limit()) buffer.limit(bufferLength) - list.add(Packet(protocolVersion, packetType, sequence, content)) + list.add(Packet(tagShort, packetType, tag, content)) } return list }