我正在制作一个匿名聊天应用程序,它需要大量的 View Controller 和聊天室。所以,我想知道如何使用安全和规则页面在 firebase 中实现 Rooms。另外,如果我想设置多个房间,我的聊天 View Controller 中的代码是什么。
我已经查看了 firebase 中的 IOS 文档,但它对我来说并没有真正的意义。
这是我的 ChatViewController 以供进一步引用。
class ChatViewController: JSQMessagesViewController {
// MARK: Properties
var messages = [JSQMessage]() // messages is an array to store the various instances of JSQMessage
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!
// MARK: Firebase refs
// In case you’re wondering, creating another reference doesn’t mean you’re creating another connection. Every reference shares the same connection to the same Firebase database.
let rootRef = Firebase(url: BASE_URL)
var messageRef = Firebase()
var userIsTypingRef: Firebase! // reference that tracks whether the local user is typing
var usersTypingQuery: FQuery! // FQuery, which is just like a Firebase reference, except that it’s ordered by an order function.
// Typing tracking related properties
private var localTyping = false // Store whether the local user is typing in a private property
var isTyping: Bool {
get {
return localTyping
}
set {
// Using a computed property, update userIsTypingRef each time user updates this property.
localTyping = newValue
userIsTypingRef.setValue(newValue)
}
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Main Chat!!"
setupBubbles()
// No avatars
collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero
messageRef = rootRef.childByAppendingPath("messages")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.observeMessages()
self.observeTyping()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
messageRef.removeAllObservers()
}
private func setupBubbles() {
// JSQMessagesBubbleImageFactory has methods that create the images for the chat bubbles. There’s even a category provided by JSQMessagesViewController that creates the message bubble colors used in the native Messages app.
let factory = JSQMessagesBubbleImageFactory()
outgoingBubbleImageView = factory.outgoingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleBlueColor())
incomingBubbleImageView = factory.incomingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleLightGrayColor())
}
// MARK: JSQMessagesCollectionView Datasource
override func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!,
messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let message = messages[indexPath.item] // retrieve the message based on the NSIndexPath item.
if message.senderId == senderId { // Check if the message was sent by the local user. If so, return the outgoing image view.
return outgoingBubbleImageView
} else { // If the message was not sent by the local user, return the incoming image view.
return incomingBubbleImageView
}
}
// set text color based on who is sending the messages
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == senderId {
cell.textView!.textColor = UIColor.whiteColor()
} else {
cell.textView?.textColor = UIColor.blackColor()
}
return cell
}
// remove avatar support and close the gap where the avatars would normally get displayed.
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
// MARK: - Create Message
// This helper method creates a new JSQMessage with a blank displayName and adds it to the data source.
func addMessage(id: String, text: String) {
let message = JSQMessage(senderId: id, displayName: "", text: text)
messages.append(message)
}
// SEND button pressed
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
let itemRef = messageRef.childByAutoId() // Using childByAutoId(), you create a child reference with a unique key.
// Create a dictionary to represent the message. A [String: AnyObject] works as a JSON-like object
let messageItem = ["text": text, "senderId": senderId]
itemRef.setValue(messageItem) // Save the value at the new child location.
// Play the canonical “message sent” sound.
JSQSystemSoundPlayer.jsq_playMessageSentSound()
// Complete the “send” action and reset the input toolbar to empty.
finishSendingMessage()
}
// Observer logic
private func observeMessages () {
// Start by creating a query that limits the synchronization to the last 25 messages.
let messagesQuery = messageRef.queryLimitedToLast(25)
// Use the .ChildAdded event to observe for every child item that has been added, and will be added, at the messages location.
messagesQuery.observeEventType(FEventType.ChildAdded) { (snapshot:FDataSnapshot!) -> Void in
// Extract the senderId and text from snapshot.value.
if let id = snapshot.value["senderId"] as? String, text = snapshot.value["text"] as? String {
// Call addMessage() to add the new message to the data source.
self.addMessage(id, text: text)
// Inform JSQMessagesViewController that a message has been received.
self.finishReceivingMessage()
}
}
}
// observer user typing object from Firebase
private func observeTyping() {
// This method creates a reference to the URL of /typingIndicator, which is where you’ll update the typing status of the user. You don’t want this data to linger around after users have logged out, so you can delete it once the user has left using onDisconnectRemoveValue().
let typingIndicatorRef = rootRef.childByAppendingPath("typingIndicator")
userIsTypingRef = typingIndicatorRef.childByAppendingPath(senderId)
userIsTypingRef.onDisconnectRemoveValue()
// initialize the query by retrieving all users who are typing. This is basically saying, “Hey Firebase, go to the key /typingIndicators and get me all users for whom the value is true.”
usersTypingQuery = typingIndicatorRef.queryOrderedByValue().queryEqualToValue(true)
// Observe for changes using .Value; this will give you an update anytime anything changes.
usersTypingQuery.observeEventType(FEventType.Value) { (data:FDataSnapshot!) -> Void in
// You're the only typing, don't show the indicator
if data.childrenCount == 1 && self.isTyping {
return
}
// Are there others typing?
self.showTypingIndicator = data.childrenCount > 0
self.scrollToBottomAnimated(true)
}
}
// Mark: textView delegate
override func textViewDidChange(textView: UITextView) {
super.textViewDidChange(textView)
// If the text is not empty, the user is typing
isTyping = textView.text != ""
}
}
最佳答案
我将回答第一个问题:是的,您可以使用“安全和规则”页面在 firebase 中实现 Rooms。除了聊天(房间)节点之外,您还必须使用 roomMembers 设置一个节点。 聊天室的安全和规则将是:
"Chat": {
"$room_id": {
// the list of messages for a room can be read by any member
".read": "root.child('RoomMembers/'+$room_id+'/'+auth.uid).exists()",
"$message_id": {
// a new message can be created if it does not exist, but it
// cannot be modified or deleted
// any member of a room can write a new message
".write": "root.child('RoomMembers/'+$room_id+'/'+auth.uid).exists() && !data.exists() && newData.exists()"
}
}
},
房间成员的安全和规则是:
"RoomMembers": {
// if the list doesn't exist, anybody can create it
// if it already exists, only users already in the list may modify it
".write": "!data.exists()",
"$room_id": {
// any member can read the list of member names
".read": "data.child(auth.uid).exists()",
".write": "!data.exists() || data.hasChild(auth.uid)"
},
// any logged in user can get a list of room names
".read": "auth !== null"
},
我在 Firebase 网站上找到了大部分内容:https://www.firebase.com/docs/security/guide/user-security.html 在 StackOverflow 上:Firebase Security rules for an app with multiple chat rooms
完整(仅针对 Chat 和 RoomMembers 部分):
{
"rules": {
"Chat": {
"$room_id": {
// the list of messages for a room can be read by any member
".read": "root.child('RoomMembers/'+$room_id+'/'+auth.uid).exists()",
//".write": true,
"$message_id": {
// a new message can be created if it does not exist, but it
// cannot be modified or deleted
// any member of a room can write a new message
".write": "root.child('RoomMembers/'+$room_id+'/'+auth.uid).exists() && !data.exists() && newData.exists()"
}
}
},
"RoomMembers": {
// if the list doesn't exist, anybody can create it
// if it already exists, only users already in the list may modify it
".write": "!data.exists()",
"$room_id": {
// any member can read the list of member names
".read": "data.child(auth.uid).exists()",
".write": "!data.exists() || data.hasChild(auth.uid)"
},
// any logged in user can get a list of room names
".read": "auth !== null"
}
}
}
希望对您有所帮助。
关于ios - 如何向这个 Firebase Swift Chat 应用添加多个房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37017821/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚