簡體   English   中英

SignalR IOS Client,Web Socket傳輸無法從服務器調用該方法

[英]SignalR IOS Client, Web Socket transport cannot invoke the method from server

我使用SignalR-ObjC Client在我的IOS應用程序和.Net服務器之間提供通信。

我可以連接longpulling並從自主交叉域服務器調用方法,沒有任何錯誤。 但由於我的應用程序需求,我必須使用WebSocket。 我有一個Singleton Manager,如:

@implementation SignalRManager
static int reconnectingTry;
+ (id)sharedManager {
    static SignalRManager *sharedHttpManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedHttpManager = [[self alloc] init];
        sharedHttpManager.hubConnection = [SRHubConnection connectionWithURL:@"http://xxx:8080/signalr"];
        sharedHttpManager.proxy = [sharedHttpManager.hubConnection createHubProxy:@"myhub"];
    });

    return sharedHttpManager;
}
+(SRHubProxy *)proxy
{
    return [[SignalRManager sharedManager] proxy];
}

+(SRHubConnection *)connection
{
    return [[SignalRManager sharedManager] hubConnection];
}

+(void)start
{
    SRWebSocketTransport *transport = [[SRWebSocketTransport alloc] init];

    [[SignalRManager connection] start:transport];

}
+(void)stop
{
    [[SignalRManager connection] stop];
}

和我調用像:

 [[SignalRManager proxy] invoke:@"Hello" withArgs:[NSArray array]];

我建立連接,服務器可以調用客戶端方法,但是當我嘗試從客戶端到服務器的調用方法時,會發生“請求失敗:錯誤請求(400)”錯誤。

似乎是SRWebSocketTransport中的協議(SRClientTransportInterface)實現的問題。

實際上是:

- (void)send:(id <SRConnectionInterface>)connection data:(NSString *)data completionHandler:(void (^)(id response, NSError *error))block;

而且必須是

- (void)send:(id <SRConnectionInterface>)connection data:(NSString *)data connectionData:(NSString *)connectionData completionHandler:(void (^)(id response, NSError *error))block; 

像子類一樣,沒有那個實現調用超類(SRHttpBasedTransport)方法,因此你得到“請求失敗:錯誤請求(400)”(是另一個http請求,而不是websocket)。

要解決此問題,只需在Pods項目中打開文件SRWebSocketTransport.m並更改實現,如下所示:

- (void)send:(id<SRConnectionInterface>)connection data:(NSString *)data connectionData:(NSString *)connectionData completionHandler:(void (^)(id response, NSError *error))block {
    [_webSocket send:data];

    if(block) {
        block(nil,nil);
    }
}

希望這有幫助。

pd:只是檢查github似乎在feature-2.0.0.beta1分支中得到修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM