Search code examples
swiftasp.net-coresignalrasp.net-core-signalrswiftr

SignalR in Swift using SwiftR - Error During Negotiation Request


I'm using .netcore with VS community edition on my Mac and would like to use SignalR for an iOS app using SwiftR

I got a working project directly from here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio-mac

and ran it locally and it worked inside of visual studio - it spun up at localhost:5000, and I even used the sample view they had to perform some SignalR commands. Everything looked great! It works!

Now I'm trying to utilize SwiftR and basically just do a simple call out to SignalR running locally to prove it works in Swift. (http://localhost:5000/chatHub)

I'm getting the error: Error during negotiation request

The output is this:

started
Error: Optional(["message": Error during negotiation request.])
disconnected

Here is my code - any ideas what's going on? I'm using the exact code Microsoft provided (which seems to run fine) but Swift isn't liking it.

In my podfile, i'm just using

pod 'SwiftR'

and in a misc viewController to test it out, I'm doing this:

func testThis()
    {
        // Client
        let connection = SignalR("http://localhost:5000/chatHub")

        let simpleHub = Hub("ChatHub")
        simpleHub.on("SendMessage") { args in
            let message = args![0] as! String
            let detail = args![1] as! String
            print("Message: \(message)\nDetail: \(detail)")
        }

        connection.addHub(simpleHub)
        connection.starting = { print("started") }
        connection.connected = { print("connected: \(connection.connectionID)") }
        connection.connectionSlow = { print("connectionSlow") }
        connection.reconnecting = { print("reconnecting") }
        connection.reconnected = { print("reconnected") }
        connection.disconnected = { print("disconnected") }
        connection.error = { error in
            print("Error: \(error)")

            if let source = error?["source"] as? String, source == "TimeoutException" {
                print("Connection timed out. Restarting...")
                connection.start()
            }
        }
        connection.start()

        do {
            try
            // Invoke server method
            simpleHub.invoke("SendMessage", arguments: ["Simple Test", "This is a simple message"])
        }
        catch {
            print(error)
        }
        // Invoke server method and handle response
          do {
            try simpleHub.invoke("SendMessage", arguments: ["Simple Test", "This is a simple message"]) { (result, error) in
            if let e = error {
                print("Error: \(e)")
            } else {
                print("Success!")
                if let r = result {
                    print("Result: \(r)")
                }
            }
        }
        }
          catch {
            print(error)
        }
    }

in viewDidLoad() i'm just calling testThis()

I even modified the .net code to allow cors like this:

services.AddCors();

and then further down

 app.UseCors(cors =>
            {
                cors.AllowAnyHeader();
                cors.AllowAnyOrigin();
                cors.AllowAnyMethod();
            });

but it still continues to say

Error: Optional(["message": Error during negotiation request.])

Really appreciate any insight anyone can offer!


Solution

  • SwiftR doesn't support ASP.NET Core SignalR, you can try https://github.com/moozzyk/SignalR-Client-Swift instead. Though this doesn't talk specifically about the iOS client ASP.NET SignalR and ASP.NET Core SignalR are different products https://learn.microsoft.com/en-us/aspnet/core/signalr/version-differences?view=aspnetcore-2.1