2017-02-19 102 views
0

我是使用loopback使用nodejs的新手。我從我的辦公室工作來創建實時聊天應用程序。我的老闆建議我使用fireloop.io,並且我總是閱讀http://docs.fireloop.io/en/api/的文檔併成功實現該功能。但問題是創建私人聊天室。我也遵循文檔中「使用子引用」的說明,但在發送消息之後,消息會廣播給連接到服務器的所有客戶端。 我的代碼是一樣的DOC:Loopback使用Fireloop.io的私人聊天室

import { Component } from '@angular/core'; 
import { RealTime } from './shared/sdk/services'; 
import { Room, Message, FireLoopRef } from './shared/sdk/models'; 
@Component(...) 
export class AppComponent { 
    private RoomReference: FireLoopRef<Room>; 
    private MessageReference: FireLoopRef<Message>; 
    private room: Room = new Room({ name: 'FireLoop Room' }); 
    private message: Room = new Message({ text: 'Test Message' }); 
    constructor(private realTime: RealTime) { 
    this.realTime 
     .onReady() 
     .subscribe(() => 
      this.RoomReference = this.realTime.FireLoop.ref<Room>(Room) 
      this.RoomReference.upsert(this.room).subscribe((instance: Room) => { 
      // Create a Child Reference 
      this.MessageReference = RoomReference.make(instance).child<Message>('messages'); 
      this.MessageReference.on('value').subscribe(
       (messages: Array<Message>) => this.logger.info(messages) 
      ); 
       MessageReference.upsert(this.message).subscribe((res: Message) => console.log(res.text)); 
      })) 
     ); 
    } 
} 

對不起,我的語言。 感謝

回答

0

最好的辦法是應用訪問控制規則:

  1. 允許公衆房不受限制的訪問(在 模型中使用一個布爾)
  2. 允許進入私人空間,讓業主
  3. 允許訪問私人房間的客人

對於規則2和3,你的代碼應該是這樣的:

{ 
    "name": "Room", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "properties": { 
    "name": { 
     "type": "string" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "user": { 
     "type": "belongsTo", 
     "model": "user", 
     "foreignKey": "ownerId" 
    }, 
    { 
    "members": { 
     "type": "hasMany", 
     "model": "user", 
     "foreignKey": "memberId" 
    } 
    }, 
    "acls": [ 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "DENY" 
    }, 
    { 
     "accessType": "READ", 
     "principalType": "ROLE", 
     "principalId": "member", 
     "permission": "ALLOW", 
     "property": "findById" 
    }, 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$owner", 
     "permission": "ALLOW" 
    } 
    ], 
    "methods": [] 
} 

有用的鏈接: