Skip to content

Instantly share code, notes, and snippets.

@moozzyk
Last active November 27, 2022 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moozzyk/be9be3881e36191891ead2980108bcc5 to your computer and use it in GitHub Desktop.
Save moozzyk/be9be3881e36191891ead2980108bcc5 to your computer and use it in GitHub Desktop.
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)")
}
}
@moozzyk
Copy link
Author

moozzyk commented Nov 17, 2019

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.

@mohammadsadra
Copy link

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)
}

@moozzyk
Copy link
Author

moozzyk commented Nov 27, 2022

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:

https://github.com/moozzyk/SignalR-Client-Swift/blob/4c35a3a6d11132d7a0022db0c4e9264b223224d2/Sources/SignalRClient/HubConnection.swift#L119-L131

Next time please open an issue in the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment