Swiftpack.co -  ericboxer/BoxNetworking as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
ericboxer/BoxNetworking
A wrapper class for easily working with CocoaAsync
.package(url: "https://github.com/ericboxer/BoxNetworking.git", from: "2.0.1")

BoxNetwrokingLogo

Wrapper classes for the wonder CocoaAsyncSocket Framework to make life easier.

Installation

Swift Package Manager

Include https://github.com/ericboxer/BoxNetworking.git using the master branch.

Manually

Copy the files to your project I guess?

Ussage

General

The communications classes are broken up by their function. By themselves they don't really do much except handle the netowrk traffic. It is up to you provided a delegate class for handling data.

UDP

UDP classes are broken up into Senders and Listeners. They do pretty much what it sounds like- the sender class sends messages and the listener class listens for messages.

import BoxNetworking

// iSendMessages is a Sender class to a host at 10.0.1.100:6789. 
// Outgoing messages should be sent on port 6543
var iSendMessages = UPDSender(toIP "10.0.1.100", toPort: 6789, usingPort:6543)

// Send a String to the given host
iSendMessages.sendSgtring(message:"Hello World!")

// Send a string to a different host (because we can)
sendString(message: "Hello to you too!", toHost:String = "10.0.1.101", port:UInt16 = 54545)

TCP

TCP is broken into into Clients and Servers. Again they works pretty much as expected.


import BoxNetworking


// iAmAClient is a Client Class to a server at 10.0.1.100:6789. Connection is automatic.
var iAmAClient = TCPCLient(toIP: "10.0.1.100", toPort: 6789, usingPort: 54545)

// Send "Hello World" to the client
iAmACLient.sendString(message: "Hello World", tag: 1)

// Close the connection
iAmCLient.close()

Logging

Both types of socket wrappers allow for a delegate class that handles logging


// Create a class that conforls to the BNLoggingDelegate Protocol
class LogHandler:BNLoggingDelegate {
    func log(logMessage:String, logLevel:BNLogLevels) {
        print("\(logLevel):: \(logMessage)")
    }
}

// Assign LogHandler to iSendMessages class. Set its log level to DEBUG
iSendMessages.setLogDelegate(to:LogHandler)
iSendMessages.setLogLevel(to: BNLogLevels.DEBUG)

// Send a message with an optional tag for tracking
iSendMessages.sendString("Hello World", tag:1)

// Console: Data with tag 1 sent

Dont Forget

App Sandboxing

appSandboxing This one gets me every. Single. Time. If you're having trouble with packets not sending ro being recevied be sure to double check your capabilities / entitlemnets (Sandboxing) for network data both incoming AND outgoing data.

FAQs

  • Does CocoaAsyncSockets really need a wrapper?

    Nope! But this makes it easer for me to use if without having the reconfigure everything every time I start a new project.
  • Why use this if Apple has provided its own socket implimentation?

    I've had challanged with the framekworks selecting when they will and wont listen to incoming data. I at least know this works exactly as I expect it to.

GitHub

link
Stars: 1
Last commit: 6 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics