2014-12-02 135 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,你已經有了一個非常安全的,可擴展的解決方案。