Asp.Net Core SignalR server: github.com/moozzyk/Sig…

  • Import tripartite library
  • Encapsulation method
  • How do I bridge code with OC
  • Code call example
  • The end of the

Import tripartite library

use_frameworks!
pod 'SwiftSignalRClient'
Copy the code

Encapsulation method

import UIKit; Import SwiftSignalRClient // The return value is a function pointer, Typealias funcBlock = (String) -> () // TypeAlias errorBlock = (String,Error?) -> () @objc class SignalRSwift: NSObject { private let dispatchQueue = DispatchQueue(label: "hubsamplephone.queue.dispatcheueuq") private var chatHubConnection: HubConnection? private var chatHubConnectionDelegate: HubConnectionDelegate? private var name = "" private var messages: [String] = [] @objc public func signalROpen(url:String,headers: [String: String]? ,hubName:String,blockfunc:funcBlock!) { name=hubName self.chatHubConnectionDelegate = ChatHubConnectionDelegate(signalrswift: self) self.chatHubConnection = HubConnectionBuilder(url: URL(string: url)!) .withLogging(minLogLevel: .debug) .withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!) .withHttpConnectionOptions(configureHttpOptions: { (httpConnectionOptions) in if let header = headers { for (key, value) in header { httpConnectionOptions.headers[key] = value } } }) .build() self.chatHubConnection! .on(method: "Message", callback: {(message: String) in // self.appendMessage(message: "\(message)") blockfunc(message) }) self.chatHubConnection! .start() } @objc public func signalRClose(){ chatHubConnection? .stop() } @objc public func sendMessage(message:String,arg:Array<String>, invocationDidComplete: @escaping (_ result: AnyObject? , _ error: Error?) -> Void) { if message ! = "" { // chatHubConnection? .invoke(method:"SendMessage",message) { error in //// if let e = error { //// self.appendMessage(message: "Error: \(e)") //// } // } // chatHubConnection? .invoke(method: "add", arg, resultType:Int.self, invocationDidComplete: { result, error in if let error = error { print("error: \(error)") } else { print("Add result: \(result!) ")}})}} fileprivate func connectionDidOpen() {// chatHubConnection? .invoke(method:"Join", name) { error in //// if let e = error { //// self.appendMessage(message: "Error: \(e)"; / / / / / /}} print (" = = = = = = = = = = = = = connection success = = = = = = = = = = = = = = ")} / / connection failure fileprivate func connectionDidFailToOpen (error: Error) { blockUI(message: "Connection failed to start.", error: The error) print (" = = = = = = = = = = = = = if connection failure = = = = = = = = = = = = = = ")} / / connection is closed fileprivate func connectionDidClose (error: error? {print (" = = = = = = = = = = = = = Connection is closed = = = = = = = = = = = = = = ") blockUI (message: "Connection is closed.", the error: Fileprivate func connectionWillReconnect(error: error?) {print (" = = = = = = = = = = = = = connection will reconnect the = = = = = = = = = = = = = = ")} / / connection whether to reconnect the fileprivate func connectionDidReconnect () { Print (" = = = = = = = = = = = = = connection whether to reconnect the = = = = = = = = = = = = = = ")} func blockUI (message: String, error: error? { var message = message if let e = error { message.append(" Error: \(e)") } // appendMessage(message: message) // toggleUI(isEnabled: false) } } class ChatHubConnectionDelegate: HubConnectionDelegate { weak var signalrswift: SignalRSwift? init(signalrswift: SignalRSwift) { self.signalrswift = signalrswift } func connectionDidOpen(hubConnection: HubConnection) { signalrswift? .connectionDidOpen() } func connectionDidFailToOpen(error: Error) { signalrswift? .connectionDidFailToOpen(error: error) } func connectionDidClose(error: Error?) { signalrswift? .connectionDidClose(error: error) } func connectionWillReconnect(error: Error) { signalrswift? .connectionWillReconnect(error: error) } func connectionDidReconnect() { signalrswift? .connectionDidReconnect() } }Copy the code

How do I bridge with OC

Import OC into SWift file will automatically generate bridge file at this time directly click ok to generate

Signalr-objc-bridge-swift-bridging -Header 2. Enable Bitcode set to YES 3. When you bridge code, you have to run it after you write it or it won't generate the.h methodCopy the code

Code call example

// open signalR NSString * token = @" XXX "; signalrSwift=[[SignalRSwift alloc]init]; NSString *signalRString =@"xxxx"; NSDictionary *headers = @{@"Authorization":token,@"Content-Type":@"application/json"}; // __weak __typeof(&*self)weakSelf = self; [signalrSwift signalROpenWithUrl:signalRString headers:headers hubName:@"" blockfunc:^(NSString * _Nonnull message) { }];Copy the code

The end of the

This project is only going to have Asp.Net Core in the background and if you’re going to write it in a different context you can look for other frameworks that have automatic reconnection. The new version does not yet know how to bridge over OC calls.