我正在连接到一个向我返回二进制数据的网络服务器。使用下面的Python代码,我能够输出结果:
from struct import unpack_from
import socket
HOST = "127.0.0.1"
PORT = 12345
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
data = s.recv(7000)
print(f"type: {type(data)}, size: {len(data)}, data: {data[0:10]}")
output result >>> type: <class 'bytes'>, size: 6985, data: b'\x00\x00\x1bE\x00\x00\x00\x00\x10\x00'
但是,当我尝试对 ktor 使用他们的示例使用以下代码执行相同操作时:
import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
import io.ktor.http.*
import io.ktor.websocket.*
import io.ktor.util.*
import kotlinx.coroutines.*
fun main() {
val client = HttpClient {
install(WebSockets)
}
runBlocking {
client.webSocket(method = HttpMethod.Get, host = "127.0.0.1", port = 12345) {
while(true) {
val othersMessage = incoming.receive() as? Frame.Binary ?: continue
println(othersMessage.readBytes())
}
}
}
client.close()
println("Connection closed. Goodbye!")
}
我被
Exception: Unsupported byte code, first byte is 0x92
击中。
不确定出了什么问题或如何调试。请问有什么想法吗?