Hi, I'm using Network Framework to make a peer-to-peer connection between two devices over a WebSocket bytestream. I was wondering, should there be any special logic in my receiveMessage(completion:) code in NWConnection, since I'm using the WebSocket protocol?
Here are some snippets of my code:
Here's my receiveMessage code:
Do WebSocket messages have additional metadata that I need to filter through in my receiveMessage function? Or does content already have just the data I want?
Here are some snippets of my code:
Code Block swift (NWConnection) var connection: NWConnection let parameters = NWParameters.tls parameters.includePeerToPeer = true let websocketOptions = NWProtocolWebSocket.Options() websocketOptions.autoReplyPing = true parameters.defaultProtocolStack.applicationProtocols.insert(websocketOptions, at: 0) let connection = NWConnection(to: endpoint, using: parameters) startConnection() func startConnection() { connection.stateUpdateHandler = { newState in switch newState { case .ready: log("Connection \(connection) established") self.receiveMessage() // Handle the rest } }
Here's my receiveMessage code:
Code Block swift func receiveMessage() { connection.receiveMessage { [unowned self] (content, context, isComplete, error) in if let error = error { log("Error while receiving data\(error)") return } if let data = content { didReceiveMessage(data) if let context?.isFinal != nil { didFinishReceiving() } } self.receiveMessage() } }
Do WebSocket messages have additional metadata that I need to filter through in my receiveMessage function? Or does content already have just the data I want?