Last active
November 27, 2022 19:01
-
-
Save moozzyk/be9be3881e36191891ead2980108bcc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Snippets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use_frameworks! | |
pod 'SwiftSignalRClient' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let hubConnection = HubConnectionBuilder(url: URL(string: "http://localhost:5000/playground")!) | |
.withLogging(minLogLevel: .debug) | |
.build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol HubConnectionDelegate: class { | |
func connectionDidOpen(hubConnection: HubConnection) | |
func connectionDidFailToOpen(error: Error) | |
func connectionDidClose(error: Error?) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!)") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hubConnection.on(method: "AddMessage") {(user: String, message: String) in | |
print(">>> \(user): \(message)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hubConnection.send(method: "Broadcast", "Playground user", "Testing send") { error in | |
if let error = error { | |
print("Send failed: \(error)") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hubConnection.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hubConnection.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I have more than 8 args in "on" function what should i do?!!!