A package that contains a client behaving as a wrapper for the URLSessionWebSocketTask
(WORK IN PROGRESS)
AsyncWebSocketClient
This is a package that contains a client behaving as a wrapper for the URLSessionWebSocketTask provided by Apple.
Usage
let client = AsyncWebSocketClient(url: URL(string: "ws://localhost:8765/")!)
try await client.connect()
try await client.send(.string("Hello world!")) // Either raw data or strings can be sent
let stream = await client.listenStream() // Returns an AsyncStream of events
for await event in stream {
print(event) // Print events such as data received, connection opened, connection closed
}
Advanced usage
You can also send encodable objects to the client:
struct MyEncodableClass: Encodable {
let title: String
}
let objectToEncode = MyEncodableClass(title: "Title")
try await client.sendJSONData(objectToEncode)
How to test
(Coming soon)
Installation
Swift Package Manager
In Xcode, select File –> Swift Packages –> Add Package Dependency and then add the following url:
https://github.com/Henryforce/AsyncWebSocketClient