NWWebSocket
使用Apple的Network框架编写的Swift WebSocket客户端。
支持的平台
- Swift 5.1及以上
- Xcode 11.0及以上
部署目标
- iOS 13.0及以上
- macOS 10.15及以上
- tvOS 13.0及以上
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。
如果您还没有安装 Cocoapods 钥匙串,请运行以下命令
$ gem install cocoapods
使用 CocoaPods 将 NWWebSocket 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '14.0'
use_frameworks!
pod 'NWWebSocket', '~> 0.5.3'
然后,运行以下命令
$ pod install
如果您发现运行 pod install
时安装的不是最新版本,那么尝试运行
$ pod cache clean
$ pod repo update NWWebSocket
$ pod install
此外,您还需要确保在您的 Podfile.lock
文件中未将 NWWebSocket 的版本锁定到旧版本。
Swift Package Manager
要使用 Swift Package Manager 将库集成到您的项目中,您可以在 Xcode 中将其作为依赖项添加 - 请参阅 文档。包仓库 URL 是
https://github.com/pusher/NWWebSocket.git
或者,您可以在您的 Package.swift
文件中将库作为依赖项添加。例如
// swift-tools-version:5.1
import PackageDescription
let package = Package(
name: "YourPackage",
products: [
.library(
name: "YourPackage",
targets: ["YourPackage"]),
],
dependencies: [
.package(url: "https://github.com/pusher/NWWebSocket.git",
.upToNextMajor(from: "0.5.3")),
],
targets: [
.target(
name: "YourPackage",
dependencies: ["NWWebSocket"]),
]
)
然后,您需要在任何您希望使用库的源文件中包含 import Network
和 import NWWebSocket
语句。
用法
本节介绍了如何配置和使用 NWWebSocket 来管理 WebSocket 连接。
连接和断开
连接和断开很简单。默认情况下,连接到 WebSocket 是手动的,将 connectAutomatically
设置为 true
则使连接自动进行。
手动连接
let socketURL = URL(string: "wss://somewebsockethost.com")
let socket = NWWebSocket(url: socketURL)
socket.delegate = self
socket.connect()
// Use the WebSocket…
socket.disconnect()
自动连接
let socketURL = URL(string: "wss://somewebsockethost.com")
let socket = NWWebSocket(url: socketURL, connectAutomatically: true)
socket.delegate = self
// Use the WebSocket…
socket.disconnect()
注意事项
- 在上述代码示例中,
self
必须符合WebSocketConnectionDelegate
(参见 接收消息和连接更新
发送数据
UTF-8 编码的字符串或二进制数据可以通过 WebSocket 连接发送。
// Sending a `String`
let message = "Hello, world!"
socket.send(string: message)
// Sending some binary data
let data: [UInt8] = [123, 234]
let messageData = Data(data)
socket.send(data: messageData)
接收消息和连接更新
字符串或数据消息(以及连接状态更新)可以通过使你定义的类型符合 WebSocketConnectionDelegate
来接收。然后,你可以根据接收到的消息或连接事件作出相应的响应。
extension MyWebSocketConnectionManager: WebSocketConnectionDelegate {
func webSocketDidConnect(connection: WebSocketConnection) {
// Respond to a WebSocket connection event
}
func webSocketDidDisconnect(connection: WebSocketConnection,
closeCode: NWProtocolWebSocket.CloseCode, reason: Data?) {
// Respond to a WebSocket disconnection event
}
func webSocketViabilityDidChange(connection: WebSocketConnection, isViable: Bool) {
// Respond to a WebSocket connection viability change event
}
func webSocketDidAttemptBetterPathMigration(result: Result<WebSocketConnection, NWError>) {
// Respond to when a WebSocket connection migrates to a better network path
// (e.g. A device moves from a cellular connection to a Wi-Fi connection)
}
func webSocketDidReceiveError(connection: WebSocketConnection, error: NWError) {
// Respond to a WebSocket error event
}
func webSocketDidReceivePong(connection: WebSocketConnection) {
// Respond to a WebSocket connection receiving a Pong from the peer
}
func webSocketDidReceiveMessage(connection: WebSocketConnection, string: String) {
// Respond to a WebSocket connection receiving a `String` message
}
func webSocketDidReceiveMessage(connection: WebSocketConnection, data: Data) {
// Respond to a WebSocket connection receiving a binary `Data` message
}
}
Ping 和 Pong
在活动的 WebSocket 连接上触发 Ping 是告诉连接的对方应该保持连接关系的最佳实践方法。Ping 可以按需或定期触发。
// Trigger a Ping on demand
socket.ping()
// Trigger a Ping periodically
// (This is useful when messages are infrequently sent across the connection to prevent a connection closure)
socket.ping(interval: 30.0)
文档
该库的完整文档可以在 API 文档 中找到。
报告错误和征求功能
- 如果您发现了错误或有一些功能请求,请提出问题。
- 如果您想贡献力量,请提交拉取请求(最好是包含一些测试用例)
🙂 )
信用
NWWebSocket 由 Pusher 所有和维护。它最初由 Daniel Browne 创建。
它使用了以下存储库的代码
许可证
NWWebSocket采用MIT许可证发布。有关详细信息,请参阅LICENSE。