我正在尝试使用SwiftR.connect实现SignalR - 我遵循了tutorial。
获得以下错误。
Starting... Error: Optional(["message": Error during negotiation request.]) Disconnected.
我的源代码
import UIKit
import SwiftR
class ViewController: UIViewController {
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
var chatHub: Hub!
var connection: SignalR!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
SwiftR.useWKWebView = true
SwiftR.signalRVersion = .v2_2_1
SwiftR.transport = .longPolling
connection = SwiftR.connect("http://192.168.X.XX/MyChatApplicationServer/signalr") { [weak self] connection in
self?.chatHub = connection.createHubProxy("MyChatHub")
self?.chatHub?.on("newMessageReceived") { args in
print("MyChatHub on call start")
let message = args![0] as! String
let detail = args![1] as! String
print("Message: \(message) \nDetail: \(detail)")
}
// SignalR events
connection.starting = { [weak self] in
self?.statusLabel.text = "Starting..."
self?.startButton.isEnabled = false
print("Starting....")
}
connection.reconnecting = { [weak self] in
self?.statusLabel.text = "Reconnecting..."
self?.startButton.isEnabled = false
print("Reconnecting....")
}
connection.connected = { [weak self] in
print("Connection ID: \(connection.connectionID!)")
self?.statusLabel.text = "Connected"
self?.startButton.isEnabled = true
self?.startButton.setTitle("Stop", for: .normal)
print("Connected")
}
connection.reconnected = { [weak self] in
self?.statusLabel.text = "Reconnected. Connection ID: \(connection.connectionID!)"
self?.startButton.isEnabled = true
self?.startButton.setTitle("Stop", for: .normal)
print("Stop")
}
connection.disconnected = { [weak self] in
self?.statusLabel.text = "Disconnected"
self?.startButton.isEnabled = true
self?.startButton.setTitle("Start", for: .normal)
print("Disconnected")
}
connection.connectionSlow = { print("Connection slow...") }
connection.error = { error in
print("Error: \(error)")
// Here's an example of how to automatically reconnect after a timeout.
//
// For example, on the device, if the app is in the background long enough
// for the SignalR connection to time out, you'll get disconnected/error
// notifications when the app becomes active again.
if let source = error?["source"] as? String, source == "TimeoutException" {
print("Connection timed out. Restarting...")
connection.start()
}
}
connection.start()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startStop(_ sender: Any) {
if let text = startButton.titleLabel?.text{
print("text val: \(text)")
connection?.start()
}else{
connection?.stop()
}
}
}
注意: 1)我使用 pod'SwiftR'更新了SignalR 安装 2)根据教程我正在使用 swift23分支代码 3)在 pod SwiftR库下面的代码不起作用。
连接= SigmalR( “http://192.168.X.XX/MyChatApplicationServer/signalr”)
获得以下错误。
"Cannot invoke initializer for type 'SignalR' with an argument list of type '(String)'"
如果我遵循主分行代码。
请帮我解决这个问题。 提前谢谢。
答案 0 :(得分:0)
检查您使用的代表this issue。
我建议考虑ObjC implementation.因为它只是pod,所以没什么不好的。
答案 1 :(得分:0)
刚刚评论了以下代码,它运行正常。 //SwiftR.useWKWebView = true
答案 2 :(得分:0)
此问题已在GitHub中讨论过。点击https://github.com/adamhartford/SwiftR/issues/75
是否适用于SwiftR.useWKWebView = false?
如果是这样,请确保在服务器上启用了CORS。 WKWebView需要CORS才能使用SwiftR。