【问题标题】:Parse + PubNub : private chatParse + PubNub:私人聊天
【发布时间】:2014-12-02 18:21:32
【问题描述】:

我想与 Parse 和 Pubnub 进行私人聊天。 当用户收到另一个朋友的图片时,他可以点击“通过消息回复”,打开一个新视图,这是两个朋友之间的私人聊天。 我在框架中使用“BubbleView”来提供 iOS 消息传递方面。 如何在 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);
}

【问题讨论】:

  • 您可以在解析时创建一个包含两个用户的聊天对象。然后,您可以将该聊天的对象 ID 用作 pubnub 频道。通过这种方式,您可以查询所有用户的聊天记录并知道要收听哪些频道。如果另一个用户开始聊天并且尚未通知该用户,这可能很有用。不确定这是否有帮助。
  • @Logan 好主意,但是如何在解析时创建一个包含两个用户的聊天对象?
  • 您可以创建新对象,例如PFObject *chat = [PFObject objectWithClassName:@"Chat"],然后添加用户:chat[@"users"] = @[[PFUser currentUser], otherUser];。设置完成后,您可以进行类似query whereKey:@"users" equalTo:[PFUser currentUser]]; 的查询,它将获取包含当前用户的所有聊天。
  • @Logan 真是个好主意,但我不知道如何存储 otherUser 的 PFUser :( 你能回复:stackoverflow.com/questions/27297817/…
  • @Logan 你能帮我存储 otherUser 的 PFUser 吗?我很高兴有这个,谢谢洛根!

标签: ios parse-platform chat private pubnub


【解决方案1】:

虽然是在 JavaScript 中,但这是一个关于如何使用 PubNub 实现聊天功能(私人 + 公共频道)的非常详细的教程:

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

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

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

要真正将 PubNub 频道设为私有,请使用 PAM 功能(如教程中所述)。这将允许您向特定频道授予和撤销授权令牌,因此即使有人猜到了私人频道名称,他们也无法在不知道身份验证令牌的情况下访问它。

要进一步锁定它,您可以使用内置加密,并且通过安全 (SSL) PubNub 连接运行,您将获得一个非常安全且可扩展的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    相关资源
    最近更新 更多