首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Parse + PubNub :私聊

Parse + PubNub :私聊
EN

Stack Overflow用户
提问于 2014-12-03 02:21:33
回答 1查看 1.2K关注 0票数 2

我想和Parse和Pubnub私下聊聊。当用户收到来自另一个朋友的图片时,他可以点击“回复消息”,这将打开一个新的视图,这是两个朋友之间的私人聊天。我在框架中使用"BubbleView“来提供iOS消息传递方面。如何在Pubnub中创建私人频道?我已经添加了

代码语言:javascript
复制
PFUser *user = [PFUser currentUser];

channel = [PNChannel channelWithName:user.objectId];

但这只会影响到正在使用应用程序的用户的通道,而不会影响这两个人的通道...?使用我的代码,我可以接收我自己的消息,控制台显示: PubNub (xxxxxxxxxx) SUBSCRIBED ON CHANNELS:(“PNChannel(Xxxxxxxxxx) objectID(user from parse who User Parse))”message received:我发送的消息

下面是我的代码:

ChatViewController.h:

代码语言:javascript
复制
#import "MessagesViewController.h"
#import "PNImports.h"

@interface ChatViewController : MessagesViewController

@property (strong, nonatomic) NSMutableArray *messages;

@end

ChatViewController.m:

代码语言:javascript
复制
#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中:

代码语言:javascript
复制
- (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);
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-04 02:22:10

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

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

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

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

要真正使PubNub通道成为私有通道,请使用PAM特性(如本教程中所述)。这将允许您授予和撤销特定通道的授权令牌,因此即使有人猜测了私有通道的名称,他们也无法在不知道auth令牌的情况下访问它。

要进一步锁定它,您可以使用内置加密,通过安全的(SSL) PubNub连接运行它,您就有了一个非常安全和可伸缩的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27256313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档