vapor / websocket-kit

WebSocket client library built on SwiftNIO
https://docs.vapor.codes/4.0/advanced/websockets/
MIT License
277 stars 79 forks source link

How send binary data? #64

Closed malcommac closed 4 years ago

malcommac commented 4 years ago

Hi, I've tried to modify the example in tests in order to send a small image as NSData.

This is the change on server side:

return WebSocket.server(on: channel) { ws in
  let data = UIImage(named: "test")!.pngData()!
  let byteArray = [UInt8](data)
  ws.send(raw: byteArray, opcode: .binary)
}

And this insie the portion of the client

WebSocket.connect(to: "ws://localhost:\(port)", on: self.elg) { ws in
  ws.onBinary { (socket, bytes) in
    let data = bytes.getData(at: 0, length: bytes.capacity)
    let imageFromData = UIImage(data: data!)
    ws.close(promise: nil)
}
}.cascadeFailure(to: promise)

However message is never received anything inside ws.onBinary. Where am I wrong?

tanner0101 commented 4 years ago

Hi @malcommac. Which version of this package are you using?

malcommac commented 4 years ago

The one on master, 7 days ago. Why?

tanner0101 commented 4 years ago

How have you configured the server channel? There's a test case similar to what you've done here: https://github.com/vapor/websocket-kit/blob/master/Tests/WebSocketKitTests/NIOWebSocketClientTests.swift#L40-L64

That appears to be working, albeit for text opcode. Not sure why binary would be any different though.