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

时间:2017-02-09 20:52:19

标签: ios objective-c signalr

我正在寻找如何正确加入群组,因为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":[]}

1 个答案:

答案 0 :(得分:2)

加入一个组是服务器端的事情。您需要创建一个集线器方法(服务器端),该方法将连接添加到组,然后从客户端调用此集线器方法。有关详细信息,请查看this article