Last active
November 30, 2020 06:57
-
-
Save standinga/a8135a3b627f0b4d9c69f8d2102d2d77 to your computer and use it in GitHub Desktop.
IOS/OSX Networking initial server
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
// | |
// Server.swift | |
// MultiConnect | |
// | |
// Created by michal on 29/11/2020. | |
// | |
import Foundation | |
import Network | |
import UIKit | |
let server = try? Server() | |
class Server { | |
let listener: NWListener | |
init() throws { | |
let tcpOptions = NWProtocolTCP.Options() | |
tcpOptions.enableKeepalive = true | |
tcpOptions.keepaliveIdle = 2 | |
let parameters = NWParameters(tls: nil, tcp: tcpOptions) | |
parameters.includePeerToPeer = true | |
listener = try NWListener(using: parameters) | |
listener.service = NWListener.Service(name: "server", type: "_superapp._tcp") | |
} | |
func start() { | |
listener.stateUpdateHandler = { newState in | |
log("listener.stateUpdateHandler \(newState)") | |
} | |
listener.newConnectionHandler = { newConnection in | |
log("listener.newConnectionHandler \(newConnection)") | |
if sharedConnection == nil { | |
sharedConnection = PeerConnection(connection: newConnection) | |
} else { | |
newConnection.cancel() | |
} | |
} | |
listener.start(queue: .main) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment