Ios 解析+;PubNub:私人聊天
我想与Parse和Pubnub进行私人聊天。 当用户收到来自另一个朋友的图像时,他可以单击“通过消息回复”,这将打开一个新视图,下面是两个朋友之间的私人聊天。 我在框架中使用“BubbleView”提供iOS消息方面。 如何在Pubnub中创建私人频道? 我补充说Ios 解析+;PubNub:私人聊天,ios,parse-platform,chat,private,pubnub,Ios,Parse Platform,Chat,Private,Pubnub,我想与Parse和Pubnub进行私人聊天。 当用户收到来自另一个朋友的图像时,他可以单击“通过消息回复”,这将打开一个新视图,下面是两个朋友之间的私人聊天。 我在框架中使用“BubbleView”提供iOS消息方面。 如何在Pubnub中创建私人频道? 我补充说 PFUser *user = [PFUser currentUser]; channel = [PNChannel channelWithName:user.objectId]; 但这只会影响到使用该应用程序的用户的频道,而不会影
PFUser *user = [PFUser currentUser];
channel = [PNChannel channelWithName:user.objectId];
但这只会影响到使用该应用程序的用户的频道,而不会影响2人的频道?
通过我的代码,我可以接收自己的消息,控制台显示:
在频道上订阅的PubNub(XXXXXXXXX):(
“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
虽然是JavaScript,但这是一个非常详细的教程,介绍了如何使用PubNub实现聊天功能(使用私有+公共频道): PubNub ObjectiveC客户端也具有相同的功能 PubNub频道本身就是频道——频道上没有表示频道是私有或公共的属性。要模拟“私有”,可以为私有频道创建难以猜测的名称(不要给出这些名称),但随着时间的推移,这并不是最安全的解决方案 要使PubNub通道真正私有化,请使用PAM功能(如本教程中所述)。这将允许您向特定通道授予和撤销授权令牌,因此即使有人猜测私有通道名称,他们也无法在不知道auth令牌的情况下访问它
要进一步锁定它,可以使用内置加密,并通过安全(SSL)运行加密PubNub connection,您已经有了一个非常安全且可扩展的解决方案。您可以在parse上创建一个包含两个用户的聊天对象。然后您可以使用该聊天的对象id作为PubNub频道。这样,您可以查询所有用户的聊天,并知道要侦听的频道。如果其他用户开始聊天,这可能会很有用并且该用户尚未收到通知。不确定这是否有帮助。@Logan是个好主意,但如何在解析时创建包含这两个用户的聊天对象?您可以创建新对象,如
PFObject*chat=[PFObject objectWithClassName:@“chat”]
然后添加用户:chat[@“users”]=@[[PFUser currentUser],otherUser]
。设置好后,您可以执行类似于查询whereKey:@“users”equalTo:[PFUser currentUser]];
的查询,它将获得包含当前用户的所有聊天记录。@Logan这是一个非常好的主意,但我不知道如何存储其他用户的PFUser:(你能回复:@Logan你能帮我储存另一个用户的PFUser吗?我很高兴拥有这个,谢谢Logan!
- (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);
}