As the client I'm sending binary data to a websocket server using URLSessionWebSocketTask. I've found that certain binaries causes the server to close the connection.
I reproduced the issue in two ways
1) Setup a local websocket server and listened to websocket messages from my application. This results in a bad frame that causes the connection to close.
2) Sent binary data to wss://echo.websocket.org , which I expect to echo the binary. The connection closes, which leads me to believe that URLSessionWebSocketTask handles binary sequences differently.
I tested two different instances, one binary that causes connection closed (sendBuggedFrame) and another that is working perfectly (sendGoodFrame). The contents are almost identical except for the 28th byte, where 109 is replaced with 113.
func sendBuggedFrame() {
let bytes: [UInt8] = [0, 0, 0, 77, 162, 225, 53, 4, 69, 83, 85, 48, 170, 225, 53, 3, 67, 77, 69, 145, 238, 53, 0, 0, 0, 0, 0, 109, 167, 64, 152, 216, 54, 1, 160, 216, 54, 1, 130, 217, 54, 9, 115, 105, 109, 117, 108, 97, 116, 111, 114, 194, 153, 75, 13, 51, 55, 49, 48, 54, 55, 51, 55, 95, 68, 69, 77, 79, 234, 153, 75, 0, 242, 153, 75, 0, 152, 182, 75, 184, 2]
var data = Data()
data.append(contentsOf: bytes)
socket.send(URLSessionWebSocketTask.Message.data(data)) { error in
if let error = error {
print("Error send: \(error)")
}
}
}
func sendGoodFrame() {
let bytes: [UInt8] = [0, 0, 0, 77, 162, 225, 53, 4, 69, 83, 85, 48, 170, 225, 53, 3, 67, 77, 69, 145, 238, 53, 0, 0, 0, 0, 0, 113, 167, 64, 152, 216, 54, 2, 160, 216, 54, 1, 130, 217, 54, 9, 115, 105, 109, 117, 108, 97, 116, 111, 114, 194, 153, 75, 13, 51, 55, 49, 48, 54, 55, 51, 55, 95, 68, 69, 77, 79, 234, 153, 75, 0, 242, 153, 75, 0, 152, 182, 75, 184, 2]
var data = Data()
data.append(contentsOf: bytes)
socket.send(URLSessionWebSocketTask.Message.data(data)) { error in
if let error = error {
print("Error send: \(error)")
}
}
}
There's little visibility of how the framework implements the websocket protocol. Any suggestions on what's going on and how I can investigate further?