Code Monkey home page Code Monkey logo

nwsocket's Introduction

NWSocket

NWSocket is a Swift class that represents a socket connection conforming to the SocketInterface. It provides a convenient interface for managing socket connections, handling connection events, and performing read and write operations.

Features

  • Connection Status: Track the current connection status with the isConnected property.
  • Delegate Pattern: Implement the SocketDelegate protocol to receive callbacks for various socket events.
  • Configurable: Customize the socket configuration, dispatch queues, and logging strategy.

Integration

Integration through CocoaPods

To integrate NetworkCompose into your Xcode project using CocoaPods, add the following to your Podfile:

pod 'NWSocket'

then run:

pod install

Integration through Swift Package Manager (SPM)

To integrate NetworkCompose using Swift Package Manager, add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/harryngict/NWSocket.git", from: "0.0.1")
],
targets: [
    .target(
        name: "YourTargetName",
        dependencies: ["NWSocket"]
    )
]

Replace "YourTargetName" with the name of your target. Then, run:

swift package update

Usage

Initialization

final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
          let socket = Socket()
          socket.delegate = self
          do {
            try socket.connect(toHost: "your host", onPort: 1234)
          } catch {
            debugPrint("Error: \(error.localizedDescription)")
          }
    }
}

extension ViewController: SocketDelegate {
  func didReady(_ socket: SocketInterface, host: String, port: UInt16) {
    debugPrint("didReady: \(socket), host: \(host), port: \(port)")
  }

  func didReceive(_ socket: SocketInterface, data: Data) {
    debugPrint("didReceive: \(socket), data: \(data)")
  }

  func didDisconnect(_ socket: SocketInterface) {
    debugPrint("didDisconnect: \(socket)")

  }

  func didConnect(_ socket: SocketInterface, 
                  withError error: Error?,
                  withTag: String) {
    debugPrint("didConnect: \(socket), error: \(String(describing: error)), withTag: \(withTag)")

  }
}

nwsocket's People

Contributors

harryngict avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.