Hi,
I'm trying to send text via the Network protocol to a websocket. I have the following code:
func send(data: Data) {
let metaData = NWProtocolWebSocket.Metadata(opcode: .text)
let context = NWConnection.ContentContext (identifier: "context", metadata: [metaData])
self.connection.send(content: data, contentContext: context, isComplete: true, completion: .contentProcessed( { error in
if let error = error {
self.connectionDidFail(error: error)
return
print("connection \(self.id) did send")
}
This sends the text to the NWConnection, however it logs an error:
__nw_frame_claim Claiming bytes failed because start (7) is beyond end (0 - 0)
(with 7 being the length of the data).
Is there something I'm doing wrong here? Any ideas?
Did you set the applicationProtocol as WebSocket before the NWConnection was created?
let params = NWParameters()
let webSocketOptions = NWProtocolWebSocket.Options()
params.defaultProtocolStack.applicationProtocols.insert(webSocketOptions, at: 0)
Matt Eaton
DTS Engineering, CoreOS
meaton3 at apple.com
Hi,
I sure did. Here's my socket setup. Still prints the warning in the console.
init(port: UInt16) {
self.port = NWEndpoint.Port(rawValue: port)!
parameters = NWParameters(tls: nil)
parameters.allowLocalEndpointReuse = true
parameters.includePeerToPeer = true
let wsOptions = NWProtocolWebSocket.Options()
wsOptions.autoReplyPing = true
parameters.defaultProtocolStack.applicationProtocols.insert(wsOptions, at: 0)
listener = try! NWListener(using: parameters, on: self.port)
The 0 - 0 means there is there is no buffer to send the data with, so something is not calculating the start and end positions of your frames correctly.
Try this when setting up your NWConnection or NWListener:
Code Block swiftlet tcpOptions = NWProtocolTCP.Options() |
tcpOptions.enableKeepalive = true |
tcpOptions.keepaliveIdle = 2 |
|
/* Setup default TLS to use with NWProtocolWebSocket */ |
let params = NWParameters(tls: init(), tcp: tcpOptions) |
|
/* Use the WebSocket Protocol */ |
let webSocketOptions = NWProtocolWebSocket.Options() |
params.defaultProtocolStack.applicationProtocols.insert(webSocketOptions, at: 0) |
This is pretty much what you have, but try this when sending data on your connection:
Code Block swiftdo { |
let data = try encoder.encode(websocketMessage) |
/* Create a context to send the Websocket data with. */ |
let message = NWProtocolWebSocket.Metadata(opcode: .text) |
let context = NWConnection.ContentContext(identifier: "send", |
metadata: [message]) |
|
connection.send(content: data, contentContext: context, isComplete: true, completion: .contentProcessed { error in |
... |
}) |
} catch { |
... |
} |
This is one way I've been able to use websockets with NWConnection and NWListener.
Matt Eaton
DTS Engineering, CoreOS
[email protected]
Hey - just checked my provides sample project on current 11.1 (20C69)
Maybe you can have a look at -> https://github.com/thieso2/WebSockDemo
it still prints that message and, I believe I followed all your advices. I'm pretty sure you'll be able reproduce the problem by running my code.
My Question:
Can you either confirm that you do not see that message when running my code -or- provide a complete runnable sample that works without emitting this message?
Thank you & Stay safe!
Thies
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the
Apple Developer Forums Participation Agreement.
Forums
Apple Developer Program
Apple Developer Enterprise Program
App Store Small Business Program
MFi Program
News Partner Program
Video Partner Program
Security Bounty Program
Security Research Device Program