EasyTCP

main

A simple and easy TCP Client in Swift.
FlorianHubl/EasyTCP

EasyTCP

A simple and easy TCP Client in Swift.

Documentation

Start TCP

let tcp = EasyTCP(hostName: "tcpbin.com", port: 4242)
tcp.start()

Send and Recieve

let result = try await tcp.send("Hello")
// Convert to String
let resultString = String(data: result, encoding: .utf8)!
print(resultString)

TLS

let tcp = EasyTCP(hostName: "tcpbin.com", port: 4243, using: .tls)

JSON-RPC

If you're using a JSON RPC, you can specify the last letter which will be send from the Server.

let tcp = EasyTCP(hostName: "tcpbin.com", port: 4242, lastLetters: "}", debug: true)

When you are using the JSON RCP, you can send and recieve objects. You have to specify the type that confirms to codable.

struct Item: Codable {
    let a: String
}

let result = try await tcp.sendJsonRpc(input: Item(a: "Hello"), output: Item.self)

Warning

If you recieve huge data packages the client will wait until all data packages arrive. If you have a slow connection please set the waitTime higher:

let tcp = EasyTCP(hostName: "tcpbin.com", port: 4242, waitTime: 0.7)

You dont have to do this if you are using the JSON-RPC.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 17:24:32 GMT-0900 (Hawaii-Aleutian Daylight Time)