SwiftAsyncWebsocket
SwiftAsyncWebsocket 是一个完全由 Swift 编写的 websocket 库。
功能
- websocket 连接和数据发送或读取
- 碎片数据接收
- 基于 TLS/SSL 的 websocket (wss)
- 子协议扩展
- 碎片数据发送
安装
1. 手动安装
SwiftAsyncWebsocket 可以按照以下步骤轻松安装
git clone https://github.com/chouheiwa/SwiftAsyncWebsocket.git
cd SwiftAsyncWebsocket
pod install
open ./SwiftAsyncWebsocket.xcworkspace
然后构建项目并将框架拖放到您的项目中
2. CocoaPods
将以下命令添加到您的 Podfile 中
pod 'SwiftAsyncWebsocket'
然后运行 pod install
3. Carthage
SwiftAsyncWebsocket 与 Carthage 兼容。要包含它,请将以下行添加到您的 Cartfile
github "chouheiwa/SwiftAsyncWebsocket"
使用方法
- 创建请求并连接
guard let url = URL(string: "ws://demos.kaazing.com/echo") else {
return
}
let request = URLRequest(url: url)
do {
let header = try RequestHeader(request: request)
let websocket = SwiftAsyncWebsocket(requestHeader: header, delegate: self, delegateQueue: DispatchQueue.main)
try websocket.connect()
} catch let error as WebsocketError {
print("\(error)")
} catch {
fatalError()
}
在实际项目中,您需要自己控制 websocket 的生命周期,并实现 websocket 的代理方法。