Created
November 30, 2020 08:00
-
-
Save standinga/5177542e5cb632086a52e80d3f2348ad to your computer and use it in GitHub Desktop.
IOS/OSX Networking initial 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 | |
var sharedConnection: Connection? | |
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)") | |
} | |
connection.start(queue: .main) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment