代码之家  ›  专栏  ›  技术社区  ›  NullHypothesis

Swift中的信号员使用SwiftR-协商请求时出错

  •  0
  • NullHypothesis  · 技术社区  · 6 年前

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

    在本地运行,在visualstudio内部运行-在localhost:5000,我甚至使用了示例视图,它们必须执行一些信号器命令。一切看起来太好了! 真管用!

    现在我想利用 SwiftR 基本上只需对本地运行的信号机做一个简单的调用,以证明它在Swift中工作( http://localhost:5000/chatHub )

    我得到一个错误:协商请求时出错

    输出如下:

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

    在我的pod文件中,我只是使用

    pod 'SwiftR'
    

    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代码,允许COR这样:

    services.AddCors();
    

    再往下走

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

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   davidfowl    6 年前

    SwiftR不支持ASP.NET核心信号器,您可以试试 https://github.com/moozzyk/SignalR-Client-Swift 相反。虽然本文没有具体讨论iOS客户端,但ASP.NET信号器和ASP.NET核心信号器是不同的产品 https://docs.microsoft.com/en-us/aspnet/core/signalr/version-differences?view=aspnetcore-2.1

        2
  •  0
  •   devhiram    5 年前

    同样的问题也发生在我身上。在我的情况下,我主持了一个 IIS上的SignalR核心应用

    在IIS上启用WebSocket为我解决了这个问题。下面是一个关于如何做到这一点的链接: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/websocket