如何使用 SignalR Objective-C 加入群组?

How to join group using SignalR Objective-C?

我正在寻找正确加入组的方法,因为 SignalR-ObjectiveC 没有很好的文档记录。我已将我的应用程序设置为以下列方式通过 SignalR 进行通信:

-(void)ConnectSignalR{

    // Connect to the service
    SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"xxx"];

    // Register for connection lifecycle events
    [hubConnection setStarted:^{
        NSLog(@"Connection Started");
    }];
    [hubConnection setReceived:^(NSString *message) {
        NSLog(@"Connection Recieved Data: %@",message);
    }];
    [hubConnection setConnectionSlow:^{
        NSLog(@"Connection Slow");
    }];
    [hubConnection setReconnecting:^{
        NSLog(@"Connection Reconnecting");
    }];
    [hubConnection setReconnected:^{
        NSLog(@"Connection Reconnected");
    }];
    [hubConnection setClosed:^{
        NSLog(@"Connection Closed");
    }];
    [hubConnection setError:^(NSError *error) {
        NSLog(@"Connection Error %@",error);
    }];
    // Start the connection
    [hubConnection start];
}

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

然后我通过在我的主视图控制器中执行以下操作来调用这些方法:

-(void)SignalR{

    WebServices *services = [[WebServices alloc] init];

    [services ConnectSignalR];

    [services callGetSRGroupNames:^(NSMutableArray *resultsArray) {
        NSLog(@"SR GROUP NAMES: %@", resultsArray);

        SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"xxx"];

        int i;
        for (i = 0; i < [resultsArray count]; i++) {
            [hubConnection setGroupsToken:resultsArray[i]];
        }
    }];
}

这是正确的吗?我收到以下回复,但不确定其正确性:

WS: websocket did receive: {"C":"s-0,94445","S":1,"M":[]}

加入群组是服务器端的事情。您需要创建一个将连接添加到组的集线器方法(服务器端),然后从客户端调用该集线器方法。查看 this article 了解更多详情。