用于 Asp.Net Core 版本 SignalR 的 Swift SignalR 客户端
在提交问题之前,请查看常见问题解答
您需要了解的关于使用 Swift SignalR 客户端的一切 在 60 分钟内
将以下行添加到您的 Podfile
use_frameworks!
pod 'SwiftSignalRClient'
然后运行
pod install
最简单的方法是使用 Xcode UI (File -> Add Packages...
)
或者,将以下内容添加到您的 Package
依赖项中
.package(url: "https://github.com/moozzyk/SignalR-Client-Swift", .upToNextMinor(from: "0.9.0")),
然后将 "SignalRClient"
包含在您的目标依赖项中。例如
.target(name: "MySwiftPackage", dependencies: ["SignalRClient"]),
将以下行添加到您的 Cartfile
github "moozzyk/SignalR-Client-Swift"
然后运行
carthage update
将 import SwiftSignalRClient
(如果您使用 Swift Package Manager,则为 import SignalRClient
) 添加到您想要在其中使用客户端的 swift 文件中。
一个典型的实现如下所示
import Foundation
import SwiftSignalRClient
public class SignalRService {
private var connection: HubConnection
public init(url: URL) {
connection = HubConnectionBuilder(url: url).withLogging(minLogLevel: .error).build()
connection.on(method: "MessageReceived", callback: { (user: String, message: String) in
do {
self.handleMessage(message, from: user)
} catch {
print(error)
}
})
connection.start()
}
private func handleMessage(_ message: String, from user: String) {
// Do something with the message.
}
}
更详细的用户指南
在 Examples
文件夹中有几个示例项目。它们包括
一个 Xcode 工作区,其中包含用于 macOS (OSX) 和 iOS 的已编译库,以及应用程序目标“ConnectionSample”、“HubSample”和“HubSamplePhone”。
一个 .Net 解决方案,单元测试和示例可以针对它运行。
TestServer
需要 .NET Core SDK 3.0.100 或更高版本。
要运行,请导航到 TestServer
文件夹并在终端中执行以下命令
% npm install
% dotnet run
在 macOS Monterey(12.0 或更高版本)上运行 TestServer
项目时,您可能会遇到错误:“Failed to bind to address http://0.0.0.0:5000: address already in use.”。这是因为 Apple 现在在该端口上宣传“AirPlay 接收器”。可以通过禁用接收器来释放此端口:导航到系统偏好设置 > 共享并取消选中AirPlay 接收器。