2017-01-20 79 views
1

我想在我的項目中使用SignalR,我不知道如何在swift中使用signalR,我安裝了SwiftR pod,但我不知道如何調用webService方法,將參數傳遞給webService並從webService獲取JSON結果,我的簡單代碼用於連接到webService並開始連接,但一切都是錯誤的,連接不成功。 這是URL:http://s.ne *** y.ir/ 這是方法應該調用: Authentication_Code_Request(串移動,字節方法) 方法是10或20 我應該得到這樣的響應: 消息 Method Code Sender在Swift中調用SignalR方法(SwiftR)

有我的代碼:

var autHub: Hub! 
    var connection: SignalR! 
    //var name: String! 

@IBAction func sendRequest(_ sender: Any) { 

     connection = SignalR("http://s.ne***y.ir/") 
     connection.signalRVersion = .v2_2_0 

     print(connection.baseUrl) 

     var methodString: String? 
     methodString = "10" 
     chatHub = Hub("autHub") 
     chatHub.on("Authentication_Code_Request") { [weak self] args in 
      if let mobile = args?[0] as? String, let method = args?[1] as? String, let mobiletext = self?.chatTextView.text, let mehodText = methodString { 
       print(method) 
       print(mobiletext) 
       print(mehodText) 
       print(mobile) 
      } 
     } 
     connection.addHub(chatHub) 

     connection.starting = { [weak self] in 

      self?.button.tintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1) 
     } 

    } 
+0

我與SignalR工作,我可以幫助O.目標C過程。不是迅速 –

+0

@AhbhishekSharma沒問題可能會有所幫助。謝謝 – ava

+0

等待我正在爲您創建一個快速應用程序並儘快發送給您。 –

回答

2

這是使用橋接報頭

在夫特使用SignalR目標C

首先,安裝莢

莢 'SignalR-ObjC'

============================= ==========================

添加目標C類命名SignalRClassViewController.h和SignalRClassViewController.m

==== ================================================== = SignalRClassViewController.h

#import <UIKit/UIKit.h> 
#import <SignalR_ObjC/SRClientTransportInterface.h> 
#import <SignalR_ObjC/SRHubProxy.h> 
#import <SignalR_ObjC/SRHubConnection.h> 
#import <SignalR_ObjC/SRConnection.h> 
#import <SignalR_ObjC/SRHubConnectionInterface.h> 

@interface SignalRClassViewController : UIViewController<SRConnectionDelegate, SRHubConnectionInterface, SRClientTransportInterface> 


// This method is used to have Single Instance. In the whole app, Life cycle makes sure you are using only one Instance of SignalR Class because of it one to one connection so we need listeners which continue to listen each time. 


+(SignalRClassViewController *)getInstance; 


-(void)SignalRConnection; 
-(void)SignalRStopCoonection; 


// Write your Own Methods 
-(void)assignUser; 
-(void)GetWebActiveClients; 
@end 

=============================================== ======== SignalRClassViewController.m

#import "SignalRClassViewController.h" 
@interface SignalRClassViewController() 
{ 
    NSMutableArray *array ; 
} 
@end 

static SRHubProxy *chat = nil; 
static SRHubConnection *hubConnection = nil; 

@implementation SignalRClassViewController 

static SignalRClassViewController *instance = nil; 


+(SignalRClassViewController *)getInstance 
{ 
    @synchronized(self) 
    { 
     if(instance==nil) 
     { 
      instance= [SignalRClassViewController new]; 
      [instance iniHUB]; 

     }else{ 
      [instance iniHUB]; 
     } 
    } 
    return instance; 
} 



-(void) iniHUB{ 


    if(hubConnection == nil){ 
     NSDictionary *parameters; 



     parameters = @{ @"UserId": @"121", 
         @"UserToken": @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJNeUhhc2giOiIyYzg5ODVmZC1jZDkyLTRjNzItOWNmYi01MWIyMWY1Y2FjZGQiLCIyYzg5ODVmZC1jZDkyLTRjNzItOWNmYi01MWIyMWY1Y2FjZGQiOjEyMX0.bDnuuroAmXvh-kifWN5jpJaSJVnMipuMD4QXon2lB-w" 
         }; 
     hubConnection = [SRHubConnection connectionWithURLString:@"http://web.abc.......xyz.it" queryString:parameters]; 
     // parameters if you want to authenticate connection 
     else use hubConnection = [SRHubConnection connectionWithURLString:@"http://web.abc.......xyz.it"]; 

     [hubConnection setDelegate:self]; 

     chat = [hubConnection createHubProxy:@"yourHubName"]; 

     // Add event for which you want to active your listener when server trigger it 
     [chat on:@"ResponseAssignUserDevice" perform:self selector:@selector(responseAssignUserDevice:)]; 

     [chat on:@"GetWebActiveClients" perform:self selector:@selector(ResponseGetWebActiveClients:)]; 

     [hubConnection start]; 
     // Start Connection 
    } 
} 



- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

#pragma mark SRConnection Delegate 

- (void)SRConnectionDidClose:(id<SRConnectionInterface>)connection{ 
    NSLog(@"Connection close"); 
    [self removeChatObserver]; 
    [self iniHUB]; 

} 

-(void) removeChatObserver{ 
    if(chat!=nil){ 
     chat=nil; 
     hubConnection=nil; 
    } 
} 

- (void)SRConnectionDidSlow:(id<SRConnectionInterface>)connection{ 
    NSLog(@"Reconnect Slow"); 

} 
- (void)SRConnectionWillReconnect:(id<SRConnectionInterface>)connection{ 
    NSLog(@"Reconnected"); 
} 

-(void)SignalRStopCoonection{ 
    // Start the connection 
    if(hubConnection!= nil){ 
     [hubConnection stop]; 
     [self removeChatObserver]; 
    } 

} 


-(void)SRConnectionDidOpen:(SRHubConnection*)connection{ 
    NSLog(@"SR Conection open"); 
    // When Connection is open call AssignUser 
    [self assignUser]; 


} 


-(void)assignUser{ 

    NSDictionary *parameters; 
    parameters = @{ @"UserId": @"121", 
        @"DeviceType": @"Mobile", 
        @"WebToken" : @"NA", 
        @"DeviceOS" : @"iPhone", 
        @"BrowserName" : @"NA" 
        }; 


    array = [[NSMutableArray alloc]init]; 
    [array addObject:parameters]; 
    [chat invoke:@"AssignUserDevice" withArgs:array]; 
    // This will invoke AssignUserDevice which is on server and you have added it selector when server will reponse on this name responseAssignUserDevice 



} 

- (void)responseAssignUserDevice:(NSString *)message { 
    // Print the message when it comes in 
    NSLog(@"SR msg %@",message); 
} 


-(void)GetWebActiveClients{ 
    array = [[NSMutableArray alloc]init]; 
    [array addObject:[NSString stringWithFormat:@"121"]]; 
    [chat invoke:@"GetWebActiveClients" withArgs:array]; 

} 

- (void)ResponseGetWebActiveClients:(NSString *)message 
{ 
    // Print the message when it comes in 
    NSLog(@"SR msg responseGetArticleByDate %@",message); 
} 




@end 

=============================== ========================

ViewController.Swift

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    @IBAction func assignUser(_ sender: Any) { 
     SignalRClassViewController.getInstance().assignUser() 
    } 

    @IBAction func GetWebActiveClients(_ sender: Any) { 
     SignalRClassViewController.getInstance().getWebActiveClients() 
    } 
} 

請檢查code here

+0

哪裏指定的參數必須在呼叫客戶端方法時發送? – ava

+1

當你從服務器調用客戶端方法的時候,你可以通過消息字符串發送它,它將是一個字典或數組。簡單地把它作爲字符串並轉換成需要的字典或數組。 –

+0

@ava它是否適合你? –