一个用 Swift 编写的 WebSocket 客户端,使用了 Apple 的 Network 框架。
CocoaPods 是 Cocoa 项目的依赖管理器。
如果您尚未安装 Cocoapods gem,请运行以下命令
$ gem install cocoapods
要将 NWWebSocket 集成到您的 Xcode 项目中,请使用 CocoaPods,在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '14.0'
use_frameworks!
pod 'NWWebSocket', '~> 0.5.4'
然后,运行以下命令
$ pod install
如果您发现运行 pod install
时未安装最新版本,请尝试运行
$ pod cache clean
$ pod repo update NWWebSocket
$ pod install
此外,您需要确保您没有在 Podfile.lock
文件中将 NWWebSocket 的版本锁定为旧版本。
要使用 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.4")),
],
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
}
}
在活动的 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。