【发布时间】: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