Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)")
}
}
@fparkar

This comment has been minimized.

Copy link

@fparkar fparkar commented Nov 17, 2019

Hello

I am using this library.

I try to invoke method

    hubConnection.invoke(method: "getUserStatus", "e30fca3b-a2b9-4200-b89c-13f68a1226b2", resultType: pStatus.self) { result, error in
        if let error = error {
            print("fapa==error: \(error)")
        } else {
            print("fapa==Add result: \(result!)")
        }
    }

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'"

@moozzyk

This comment has been minimized.

Copy link
Owner Author

@moozzyk 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.