2014-12-02 141 views
2

我想和解析和Pubnub私人聊天。 当用户收到另一位朋友的图像时,他可以点击“通过消息回复”,打开一个新视图,这里是两个朋友之间的私人聊天。 我在框架中使用“BubbleView”来给出iOS消息传递方面。 如何在Pubnub中创建私人频道? 我已经添加解析+ PubNub:私人聊天

PFUser *user = [PFUser currentUser]; 

channel = [PNChannel channelWithName:user.objectId]; 

但只影响了渠道谁正在使用的应用程序的用户,而不是渠道为2人...? 用我的代码,我可以收到我自己的消息,控制台说: PubNub(xxxxxxxxxx)订阅频道:( “PNChannel(xxxxxxxxx)objectID(用户从解析谁使用的应用程序)” 消息收到:消息我“已传送

这里是我的代码:

ChatViewController.h:

#import "MessagesViewController.h" 
#import "PNImports.h" 

@interface ChatViewController : MessagesViewController 

@property (strong, nonatomic) NSMutableArray *messages; 

@end 

ChatViewController.m:

#import "ChatViewController.h" 
#import <Parse/Parse.h> 

@interface ChatViewController() 

@end 
PNChannel *channel; 
id message; 
NSDate *receiveDate; 
NSString *text; 
@implementation ChatViewController 

#pragma mark - View lifecycle 
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    self.title = @"Messages"; 
    self.messages = [[NSMutableArray alloc] initWithObjects: 
        @"Testing some messages here.", @"lol", 
        nil]; 
    UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [exitButton addTarget:self action:@selector(backToInboxView) forControlEvents:UIControlEventTouchUpInside]; 
    [exitButton setTitle:@"Inbox" forState:UIControlStateNormal]; 
    exitButton.frame = CGRectMake(0.0, 0.0, 60, 60); 
    [self.view addSubview:exitButton]; 

    // #1 Define client configuration 
    PNConfiguration *myConfig = [PNConfiguration configurationForOrigin:@"pubsub.pubnub.com" 
                  publishKey:@"demo" 
                  subscribeKey:@"demo" 
                   secretKey:nil]; 
    // #2 make the configuration active 
    [PubNub setConfiguration:myConfig]; 
    // #3 Connect to the PubNub 
    [PubNub connect]; 

    } 


#pragma mark - Table view data source 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.messages.count; 
} 

#pragma mark - Messages view controller 
- (BubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return (indexPath.row % 2) ? BubbleMessageStyleIncoming : BubbleMessageStyleOutgoing; 
} 

- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [self.messages objectAtIndex:indexPath.row]; 
} 

- (void)sendPressed:(UIButton *)sender withText:text 
{ 
    [self.messages addObject:text]; 
    if((self.messages.count - 1) % 2) 
     [MessageSoundEffect playMessageSentSound]; 
    else 
     [MessageSoundEffect playMessageReceivedSound]; 
    PFUser *user  = [PFUser currentUser]; 
    channel = [PNChannel channelWithName:user.objectId]; 
    // Receive Messages Sent to Me! 
    [PubNub subscribeOnChannel:channel]; 
    // Send a Message to Sally 
    [PubNub sendMessage:text toChannel:channel]; 
    [self finishSend]; 
} 

- (void)backToInboxView{ 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 


@end 

和Appdelegate.m:

- (void)pubnubClient:(PubNub *)client didSubscribeOnChannels:(NSArray *)channels { 
    NSLog(@"DELEGATE: Subscribed to channel:%@", channels); 
} 
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message { 
    NSLog(@"Message received: %@", message.message); 
} 
+0

您可以在包含两个用户的解析中创建一个聊天对象。然后你可以使用该聊天的对象ID作为pubnub频道。通过这种方式,您可以查询所有用户聊天记录,并了解要监听的频道。如果其他用户开始聊天并且该用户还没有得到通知,这可能很有用。不知道这是否有帮助。 – Logan 2014-12-02 18:33:02

+0

@Logan不错的主意,但如何创建一个包含两个用户的解析聊天对象? – Viny76 2014-12-02 18:44:55

+0

你可以只创建新的对象,如'PFObject *聊天= [PFObject objectWithClassName:@ “聊天”]'然后添加用户:'聊天[@ “用户”] = @ [PFUser currentUser],otherUser]'。一旦设置好了,你可以做一个查询,比如'query whereKey:@“users”equalTo:[PFUser currentUser]];'它将获得当前用户所在的所有聊天记录。 – Logan 2014-12-02 18:50:29

回答

1

虽然在JavaScript中,这是如何实现聊天功能非常详细的教程(私人+公共通道)PubNub:

http://www.pubnub.com/blog/javascript-private-chat-api-with-access-control/

PubNub ObjectiveC客户端具有相同的功能。

PubNub单独渠道只是渠道 - 有是说,一个通道是私人或公共通道上没有属性。要模拟“私人”,您可以为私人频道创建难以猜测的名称(并且不要指定这些名称),但随着时间的推移,这不是最安全的解决方案。

真正做出PubNub通道专用,使用PAM功能(如教程详细说明)。这将允许您授权和撤销特定频道的授权令牌,因此即使有人猜测私人频道名称,也无法在不知道授权令牌的情况下访问它。

为了更锁定下来,你可以使用内置的加密,并与运行在安全(SSL)连接PubNub,你已经有了一个非常安全的,可扩展的解决方案。