SignalR IOS Client, Web Socket transport cannot in

2019-03-21 23:55发布

I m using SignalR-ObjC Client to provide communication between my IOS application and .Net server.

I can connect with longpulling and invoke methods from self-host cross domain server without any error. But because of my applications needs i have to use WebSocket. I have a Singleton Manager like :

@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];
}

and i m invoking like :

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

i establish connection and server can invoke client method but when i try invoke method from client to server the "Request failed:bad request(400)" error occur.

1条回答
虎瘦雄心在
2楼-- · 2019-03-22 00:07

seems to be a problem with the protocol (SRClientTransportInterface) implementation in SRWebSocketTransport.

Actually is:

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

and must be

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

Like subclass does not have that implementation is calling superclass (SRHttpBasedTransport) method and for that reason you got "Request failed:bad request(400)" (is another http request and not websocket).

To fix just open the file SRWebSocketTransport.m in your Pods project and change the implementation, something like this:

- (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);
    }
}

Hope this help.

pd: just checking github seems to be fixed in feature-2.0.0.beta1 branch

查看更多
登录 后发表回答