Last active
December 24, 2022 13:09
-
-
Save michael94ellis/8d3171fbef897c9af8db7b7491d97601 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// UDPClient.swift | |
// | |
import Network | |
import Foundation | |
protocol UDPListener { | |
func handleResponse(_ client: UDPClient, data: Data) | |
} | |
class UDPClient { | |
var connection: NWConnection | |
var address: NWEndpoint.Host | |
var port: NWEndpoint.Port | |
var delegate: UDPListener? | |
var resultHandler = NWConnection.SendCompletion.contentProcessed { NWError in | |
guard NWError == nil else { | |
print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)") | |
return | |
} | |
} | |
init?(address newAddress: String, port newPort: Int32) { | |
guard let codedAddress = IPv4Address(newAddress), | |
let codedPort = NWEndpoint.Port(rawValue: NWEndpoint.Port.RawValue(newPort)) else { | |
print("Failed to create connection address") | |
return nil | |
} | |
address = .ipv4(codedAddress) | |
port = codedPort | |
connection = NWConnection(host: address, port: port, using: .udp) | |
connection.stateUpdateHandler = { newState in | |
switch (newState) { | |
case .ready: | |
print("State: Ready") | |
return | |
case .setup: | |
print("State: Setup") | |
case .cancelled: | |
print("State: Cancelled") | |
case .preparing: | |
print("State: Preparing") | |
default: | |
print("ERROR! State not defined!\n") | |
} | |
} | |
connection.start(queue: .global()) | |
} | |
deinit { | |
connection.cancel() | |
} | |
func send(_ data: Data) { | |
self.connection.send(content: data, completion: self.resultHandler) | |
self.connection.receiveMessage { data, context, isComplete, error in | |
guard let data = data else { | |
print("Error: Received nil Data") | |
return | |
} | |
guard self.delegate != nil else { | |
print("Error: UDPClient response handler is nil") | |
return | |
} | |
self.delegate?.handleResponse(self, data: data) | |
} | |
} | |
} |
This file contains 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
class UDPMessageReader: UDPListener { | |
var commandClient = UDPClient(address: IPAddress, port: CommandPort) | |
init() { | |
commandClient.delegate = self | |
} | |
func handleResponse(_ client: UDPClient, data: Data) { | |
// Do Something | |
} | |
} |
Hii Michael,
I am using IPad Pro with swiftplayground app.. I do not have access to MacBook n Xcode..
Can you please share some snippet about how to encapsulate udp sender receiver class you hv made in to swiftplayground app on iPad to make use of it.. I hv just migrated to Apple echo system and want to communicate with Tello drone via udp using swiftplayground app on ipad
Marry Christmas in advance..
Cheers
nkmaker
You probably need to import the network framework made by apple to
playgrounds, have you tried that?
…On Sat, Dec 24, 2022 at 12:56 AM nkmakerin ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hii Michael,
I am using IPad Pro with swiftplayground app.. I do not have access to
MacBook n Xcode..
Can you please share some snippet about how to encapsulate udp sender
receiver class you hv made in to swiftplayground app on iPad to make use of
it.. I hv just migrated to Apple echo system and want to communicate with
Tello drone via udp using swiftplayground app on ipad
Marry Christmas in advance..
Cheers
nkmaker
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/8d3171fbef897c9af8db7b7491d97601#gistcomment-4412413>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGEF4OFJ2TDPMYOT3SX4UDDWO2GB7BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMZVGQYTQMZVU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍🏻