SwiftSignalRClient

用于 Asp.Net Core 版本 SignalR 的 Swift SignalR 客户端

在提交问题之前,请查看常见问题解答

新增 - Swift SignalR 客户端课程

您需要了解的关于使用 Swift SignalR 客户端的一切 在 60 分钟内

安装

Cocoapods

将以下行添加到您的 Podfile

use_frameworks!
pod 'SwiftSignalRClient'

然后运行

pod install

Swift Package Manager

最简单的方法是使用 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"]),

Carthage

将以下行添加到您的 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 文件夹中有几个示例项目。它们包括

点击数

HitCount