Swift 中的 SignalR 使用 SwiftR - 协商请求期间出错

SignalR in Swift using SwiftR - Error During Negotiation Request

我在我的 Mac 上使用 .netcore 和 VS 社区版,并想将 SignalR 用于 iOS 应用 SwiftR

我直接从这里得到了一个工作项目: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio-mac

和 运行 它在本地运行,它在 visual studio 内部工作 - 它在 localhost:5000 处旋转,我什至使用了他们必须执行一些 SignalR 命令的示例视图。一切看起来都很棒! 有效!

现在我正在尝试利用 SwiftR and basically just do a simple call out to SignalR running locally to prove it works in Swift. (http://localhost:5000/chatHub)

我收到错误:协商请求期间出错

输出是这样的:

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

这是我的代码 - 知道发生了什么吗?我使用的是 Microsoft 提供的确切代码(似乎 运行 不错)但 Swift 不喜欢它。

在我的播客文件中,我只是使用

pod 'SwiftR'

在杂项 viewController 中进行测试,我正在这样做:

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

在 viewDidLoad() 中我只是调用 testThis()

我什至修改了 .net 代码以允许这样的 cors:

services.AddCors();

然后再往下

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

但它仍然继续说

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

非常感谢任何人可以提供的任何见解!

SwiftR不支持ASP.NET Core SignalR,你可以试试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://docs.microsoft.com/en-us/aspnet/core/signalr/version-differences?view=aspnetcore-2.1

我也遇到了同样的问题。在我的例子中,我在 IIS 上托管了一个 SignalR Core 应用程序,试图通过 SwiftR 和 Moozzyk 的客户端访问它。原来我的主机上没有启用 WebSockets,这是一个交易破坏者,因为引用的客户端只使用 WebSockets(没有回退到其他传输方法,如服务器发送事件、长轮询等)。

在 IIS 上启用 WebSockets 解决了我的问题。 link 如何操作: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/websocket