我正在使用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分支中修复