Swift Package Index

WebSocket

https://github.com/shareup/websocket-apple.git


Compatibility

  • v2.2.2 and master
    5.3
    5.2
    5.1
    5.0
    4.2
  • v2.2.2 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS

WebSocket wrapper around URLSessionWebSocketTask

(macOS, iOS, iPadOS, tvOS, and watchOS)

A concrete implementation of a WebSocket client implemented by wrapping Apple's URLSessionWebSocketTask and conforming to WebSocketProtocol. WebSocket exposes a simple API and conforms to Apple's Combine Publisher.

Usage

let socket = WebSocket(url: url(49999))

let sub = socket.sink(
	receiveCompletion: { print("Socket closed: \(String(describing: $0))") },
	receiveValue: { (result) in
		switch result {
		case .success(.open):
			socket.send("First message")
		case .success(.string(let incoming)):
			print("Received \(incoming)")
		case .failure:
			socket.close()
		default:
			break
		}
	}
)
defer { sub.cancel() }

socket.connect()

Tests

  1. In your Terminal, navigate to the websocket-apple directory
  2. Run the tests using swift test