我想实现SignalR使用SwiftR.connect -我跟着这个教程 。
获得下面的错误。
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)我用荚“SwiftR” 2)按照教程中,我使用swift23分支代码 3)在吊舱SwiftR库下面的代码不能正常工作了更新SignalR 安装 。
连接= SigmalR( “ HTTP://192.168.X.XX/MyChatApplicationServer/signalr ”)
获得下面的错误。
"Cannot invoke initializer for type 'SignalR' with an argument list of type '(String)'"
如果我下面的主分支代码。
请帮我解决一下这个。 提前致谢。