Skip to content

litoarias/SocketStream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SocketStreamService

Iterating over web socket messages with async / await in Swift

🚀 Usage of SocketStreamService instance, this wrapper uses a native way with URLSessionWebSocketTask and works as an asynchronous (async) functions into Swift, allowing us to run complex asynchronous code almost is if it were synchronous. You don't need to make logs, SocketStreamService will emit all necessary logs.

✏️ Example of usage:

 // CONNECT
 let url = URL(string: "wss://socketsbay.com/wss/v2/1/demo/")!
 let stream = SocketStreamService(url: url)
 stream.onConnected = {
     debugPrint("Socket connection")
 }
 
 stream.onDisconnect = {
     debugPrint("Socket disconnection")
 }
 
 // SEND MESSAGE
 Task {
    try await stream.send(text: "Test desde app ios")
 }
 
 // RECIVE MESSAGES
 Task {
     do {
         for try await message in stream {
         switch message {
         case .data(let data):
            debugPrint(String(data: data, encoding: .utf8) as Any)
         case .string(let string):
            debugPrint(string)
         @unknown default:
            fatalError()
         }
         }
      } catch {
         print("Error: \(error)")
      }
 }

About

Handle sockets with swift async/await mode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages