Last active
November 30, 2020 12:06
-
-
Save standinga/129e53f0f0750c430bc112ef3827f0e8 to your computer and use it in GitHub Desktop.
IOS/OSX Networking updated 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 | |
var connections: [Connection] = [] | |
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 = { [weak self] newConnection in | |
log("listener.newConnectionHandler \(newConnection)") | |
let connection = Connection(connection: newConnection) | |
self?.connections += [connection] | |
} | |
listener.start(queue: .main) | |
} | |
func send() { | |
connections.forEach { | |
$0.send("super message from the server! \(Int(Date().timeIntervalSince1970))") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment