Created
November 30, 2020 12:08
-
-
Save standinga/ac6316012737f42f6a3b774f4a9173c5 to your computer and use it in GitHub Desktop.
IOS/OSX Networking updated connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PeerConnection.swift | |
// MultiConnect | |
// | |
// Created by michal on 29/11/2020. | |
// | |
import Foundation | |
import Network | |
class Connection { | |
let connection: NWConnection | |
// outgoing connection | |
init(endpoint: NWEndpoint) { | |
log("PeerConnection outgoing endpoint: \(endpoint)") | |
let tcpOptions = NWProtocolTCP.Options() | |
tcpOptions.enableKeepalive = true | |
tcpOptions.keepaliveIdle = 2 | |
let parameters = NWParameters(tls: nil, tcp: tcpOptions) | |
parameters.includePeerToPeer = true | |
connection = NWConnection(to: endpoint, using: parameters) | |
start() | |
} | |
// incoming connection | |
init(connection: NWConnection) { | |
log("PeerConnection incoming connection: \(connection)") | |
self.connection = connection | |
start() | |
} | |
func start() { | |
connection.stateUpdateHandler = { newState in | |
log("connection.stateUpdateHandler \(newState)") | |
if case .ready = newState { | |
self.receiveMessage() | |
} | |
} | |
connection.start(queue: .main) | |
} | |
func send(_ message: String) { | |
connection.send(content: message.data(using: .utf8), contentContext: .defaultMessage, isComplete: true, completion: .contentProcessed({ error in | |
log("Connection send error: \(String(describing: error))") | |
})) | |
} | |
func receiveMessage() { | |
connection.receive(minimumIncompleteLength: 1, maximumLength: 100) { data, _, _, _ in | |
if let data = data, | |
let message = String(data: data, encoding: .utf8) { | |
log("Connection receiveMessage message: \(message)") | |
} | |
self.receiveMessage() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment