Snippets |
use_frameworks! | |
pod 'SwiftSignalRClient' |
let hubConnection = HubConnectionBuilder(url: URL(string: "http://localhost:5000/playground")!) | |
.withLogging(minLogLevel: .debug) | |
.build() |
public protocol HubConnectionDelegate: class { | |
func connectionDidOpen(hubConnection: HubConnection) | |
func connectionDidFailToOpen(error: Error) | |
func connectionDidClose(error: Error?) | |
} |
// invoking a hub method and receiving a result | |
hubConnection.invoke(method: "Add", 2, 3, resultType: Int.self) { result, error in | |
if let error = error { | |
print("error: \(error)") | |
} else { | |
print("Add result: \(result!)") | |
} | |
} | |
// invoking a hub method that does not return a result | |
hubConnection.invoke(method: "Broadcast", "Playground user", "Sending a message") { error in | |
if let error = error { | |
print("error: \(error)") | |
} else { | |
print("Broadcast invocation completed without errors") | |
} | |
} |
hubConnection.invoke(method: "Add", arguments: [2, 3], resultType: Int.self) { result, error in | |
if let error = error { | |
print("error: \(error)") | |
} else { | |
print("Add result: \(result!)") | |
} | |
} |
hubConnection.on(method: "AddMessage") {(user: String, message: String) in | |
print(">>> \(user): \(message)") | |
} |
hubConnection.on(method: "AddMessage", callback: { argumentExtractor in | |
let user = try argumentExtractor.getArgument(type: String.self) | |
var message = "" | |
if argumentExtractor.hasMoreArgs() { | |
message = try argumentExtractor.getArgument(type: String.self) | |
} | |
print(">>> \(user): \(message)") | |
}) |
hubConnection.send(method: "Broadcast", "Playground user", "Testing send") { error in | |
if let error = error { | |
print("Send failed: \(error)") | |
} | |
} |
hubConnection.start() |
hubConnection.stop() |
let streamHandle = hubConnection.stream(method: "StreamNumbers", 1, 10000, itemType: Int.self, | |
streamItemReceived: { item in print(">>> \(item!)") }) { error in | |
print("Stream closed.") | |
if let error = error { | |
print("Error: \(error)") | |
} | |
} | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) { | |
hubConnection.cancelStreamInvocation(streamHandle: streamHandle) { error in | |
print("Canceling stream invocation failed: \(error)") | |
} | |
} |
The getUserStatus
status method is supposed to return pStatus
but it returned null
hence the error 'while getting result for server method with invocationId 1'.
"No handler registered for method 'partnerStatus'" means that the server invoked a client method but there was no code registered to handle this invocation. You register handlers with the HubConnection.on
method.
Hi
I have more than 8 args in "on" function what should i do?!!!
func on<T1: Decodable, T2: Decodable, T3: Decodable, T4: Decodable, T5: Decodable, T6: Decodable, T7: Decodable, T8: Decodable>(method: String, callback: @escaping (_ arg1: T1, _ arg2: T2, _ arg3: T3, _ arg4: T4, _ arg5: T5, _ arg6: T6, _ arg7: T7, _ arg8: T8) -> Void) {
let cb: (ArgumentExtractor) throws -> Void = { argumentExtractor in
let arg1 = try argumentExtractor.getArgument(type: T1.self)
let arg2 = try argumentExtractor.getArgument(type: T2.self)
let arg3 = try argumentExtractor.getArgument(type: T3.self)
let arg4 = try argumentExtractor.getArgument(type: T4.self)
let arg5 = try argumentExtractor.getArgument(type: T5.self)
let arg6 = try argumentExtractor.getArgument(type: T6.self)
let arg7 = try argumentExtractor.getArgument(type: T7.self)
let arg8 = try argumentExtractor.getArgument(type: T8.self)
callback(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
}
self.on(method: method, callback: cb)
}
If you control your server you can reduce the number of parameters (e.g. by creating struct). If you don't control the server then you can just create an extension method copy what you have above and add more parameters. You can also just register this callback directly on HubConnection as the .on
extension methods are just syntactical sugar to doing this directly:
Next time please open an issue in the repo.
Hello
I am using this library.
I try to invoke method
I want to read the response.
From below link, I create the class & set it as resultType: pStatus.self in above method.
https://gist.github.com/moozzyk/be9be3881e36191891ead2980108bcc5
I get response as below.
2019-11-17T11:53:58.879Z debug: Message received: {"type":1,"target":"partnerStatus","arguments":[{"UserId":"e30fca3b-a2b9-4200-b89c-13f68a1226b2","Online":false,"Status":"Last Seen 08/11/2019 07:19 ص","LastSeen":"2019-11-08T07:19:58.8444713"}]}
2019-11-17T11:53:58.879Z debug: Message received: {"type":3,"invocationId":"1","result":null}
Also I get below log.
2019-11-17T11:53:58.879Z debug: Server method with invocationId 1 completed successfully
2019-11-17T11:53:58.879Z error: Error while getting result for server method with invocationId 1
fapa==error: serializationError(underlyingError: Swift.DecodingError.valueNotFound(Swift.KeyedDecodingContainer<ChatTestSignalR.pStatus.(unknown context at $10924b7e0).CodingKeys>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "result", intValue: nil)], debugDescription: "Cannot get keyed decoding container -- found null value instead.", underlyingError: nil)))
I am not understanding why I didn't get response for my main method of getUserStatus?
I also get message as "No handler registered for method 'partnerStatus'"