我正在尝试设置一个iOS客户端,以便将SignalR-ObjC与我们的Web服务一起使用。当我们使用长轮询方法时,一切似乎都有效,但它在我们的生产环境中存在问题。所以我被指示切换到使用Web套接字方法。基于我在网上看过的代码示例,我认为应该使用我所拥有的代码,但服务通过webSocket:didFailWithError:
方法返回错误。这是错误:
Error Domain=org.lolrus.SocketRocket Code=2132 "received bad response code from server 403" UserInfo={NSLocalizedDescription=received bad response code from server 403}
这是我在AppDelegate文件中找到的用于设置连接的代码:
<!-- language:objective c -->
-(void)initializePushNotificationsConnection
{
// Connect to the hub on the service
SRHubConnection* localHubConnection = [SRHubConnection connectionWithURL:kSignalRServiceUrl];
SRHubProxy* notifications = (SRHubProxy*)[localHubConnection createHubProxy:@"notificationHub"];
[notifications on:@"notificationSent" perform:self selector:@selector(addMessage:)];
localHubConnection.started = ^{
NSString* userGuid = [OURAppSettings sharedSettings].loginModel.theUser.userGuid;
if (!userGuid)
userGuid = @"";
NSString* connId = self.hubConnection.connectionId;
if (!connId)
connId = @"";
NSDictionary* connectionDict = @{@"UserGuid": userGuid, @"Model": connId};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:connectionDict options:0 error:nil];
NSURL* connectionRegistrationServiceUrl = [NSURL URLWithString:kSignalRConnectionRegistrationUrl];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:connectionRegistrationServiceUrl];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError)
{
if (connectionError)
{
NSLog(@"Error: %@", connectionError);
return;
}
}];
return;
};
//Set the object to the local property to break the strong connection so the block can reference the object
self.hubConnection = localHubConnection;
SRWebSocketTransport *webSocket = [[SRWebSocketTransport alloc] init];
[self.hubConnection start:webSocket];
}
我已经尝试过Github和SO的多个建议,但他们都没有解决这个问题。我怀疑Web服务的设置方式存在问题,但我不是IIS专家,也不能轻松访问设置。我现在基本上只是猜测解决方案,所以如果有人有任何想法或建议,我愿意听取他们的意见。