-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onBinary Usage? #79
Comments
Text and Binary are two different opcodes in the websocket protocol, the two handler functions simply reflect that. If the client sends a text message, it is routed to onText, and binary messages are routed to onBinary. How you actually get binary messages to be sent depends on the client, in JS as far as I can tell, you'll get binary messages if you send ArrayBuffers instead of strings directly. (The |
Thanks @vzsg, that makes sense. In the Swift-NIO WebSocket example here, the |
Hey, I think it's still a solid feature request. |
Hello, I tried to use onText to receive messages, but there was no response. This is the code I wrote. I don't know if I made a mistake. Can you give me a complete demo? Thank you very much. import Vapor public func configure(_ app: Application) throws { try webs(app) try routes(app) } func webs(_ app: Application) throws { let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) WebSocket.connect(to: "ws://echo.websocket.org", on: eventLoopGroup ) { ws in ws.onText { ws, text in print(text) } ws.onBinary { ws, binary in print(binary) } } } |
@shenfu1991 There are a few mistakes there, including creating a new MultiThreadedEventLoopGroup instead of using the Using private func webs(_ app: Application) {
WebSocket.connect(to: "wss://echo.websocket.org", on: app.eventLoopGroup) { (ws) in
print("CONNECTED!")
ws.onText { _, text in
print("Received text: \(text)")
}
ws.onBinary { _, bin in
print("Received binary message: \(bin)")
}
ws.send("Sup!")
ws.send([1,2,3,4,5])
}
} Results in the following output:
That all being said, please don't hijack other issues with questions. |
Closing this as I think it's agreed it's solved (and likely further amplified by #116 ) |
Ubuntu 18.04, websocket-kit 2.1.1
Swift for Tensorflow 0.10
I have been successfully using the
onText
callback to parse the messages my web socket client receives. However, when I replace this withonBinary
, I never receive anything. It's never called. I expectedonBinary
would be called with the same data simply as aByteBuffer
rather than first decoding it to aString
. What is the expected usage ofonBinary
? If this is not a bug, in what cases isonBinary
expected to be called?I would prefer to access the data via the original
ByteBuffer
rather than as a convertedString
because I will have to convert aString
back to bytes/Data
to do my decoding.The text was updated successfully, but these errors were encountered: