Error message when sending text to WebSocket

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)
    }
I have the same issue. has there been any solution or can I ignore that message (BTW: similar thing happens with BigSur)
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 swift
let 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 swift
do {
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
meaton3@apple.com
Hi, I have created a minimal Swift package that reproduces the error-message.

Maybe you can have a look at -> https://github.com/thieso2/WebSockDemo and

Thank you for your support!

Stay safe!
Thies from Germany.
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

I'm getting the same thing. Apart from the warning, things seem to work OK.
Seems to happen only when sending on the listening side, not the client side.
Error message when sending text to WebSocket
 
 
Q