我的swift项目中有SignalR-ObjC的问题。我创建了与SR到我的集线器的连接,代码是:
import UIKit
import SignalR_ObjC
class ViewController: UIViewController, SRConnectionDelegate {
@IBOutlet weak var textFied_Username: UITextField!
@IBOutlet weak var textField_Password: UITextField!
@IBOutlet weak var btn_Login: UIButton!
var hubConnection = SRHubConnection(URLString: "http://myUrl.com");
var hubProxy = SRHubProxy();
var txt_Username : String = "username";
var txt_Password : String = "password";
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btn_Login(sender: AnyObject) {
txt_Username = textFied_Username.text!;
txt_Password = textField_Password.text!;
hubProxy = SRHubProxy(connection: hubConnection, hubName: "MyHub");
let signInSelector = Selector("signInResponse:");
hubProxy.on("signIn", perform: self, selector: signInSelector);
hubConnection.delegate = self;
hubConnection.start();
self.SRConnectionDidOpen(hubConnection);
}
func SRConnectionDidOpen(connection: SRConnectionInterface!) {
hubProxy.invoke("SignIn", withArgs: ["username","password"])
}
func signInResponse(response: String) {
NSLog(response);
}
}
建立连接并且调用有效,但它没有收到消息,方法如下:
hubProxy.on("signIn", perform: self, selector: signInSelector);
不起作用和选择器:
signInSelector
永远不会打电话给。任何人都可以帮我解决这个问题吗? 谢谢你,抱歉我的英文不好,我希望问题很清楚。
答案 0 :(得分:0)
Swift 4,Xcode 9
尝试在func声明中添加@objc选择器,这是我的工作示例:
func configureProxy() {
let proxy: SRHubProxy? = createHubProxy(kHubName) as? SRHubProxy
// we are using proxy-connection, to avoid parsing NSData ourselves.
proxy?.on(kReceivedMessageSignalREventName, perform: self, selector: #selector(self.didReceiveMessage(_:)))
}
@objc func didReceiveMessage(_ message: [AnyHashable: Any]?) {
if (receiveMessageBlock != nil) {
receiveMessageBlock!(message)
}
}
答案 1 :(得分:-1)
hubConnection?.received = { [weak self] received in
print("Error: \(received)")
}