使用Fireloop.io的Loopback私人聊天室

时间:2017-02-19 14:40:48

标签: angularjs node.js loopbackjs fireloop

我是使用带有Loopback的nodejs的新手。我在办公室工作,创建实时聊天应用程序。我的老板建议我使用fireloop.io,我总是从http://docs.fireloop.io/en/api/阅读文档并成功实现。但问题是要创建私人聊天室。我还按照文档中“使用子引用”的说明进行操作,但在发送消息后,消息将广播给连接到服务器的所有客户端。 我的代码与doc:

相同
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));
            }))
        );
  }
}

对不起我的语言。 感谢

1 个答案:

答案 0 :(得分:0)

最好的方法是应用访问控制规则:

  1. 允许不受限制地访问公共房间(在您的房间中使用布尔值) 模型)
  2. 允许所有者访问私人房间
  3. 允许客人进入私人房间
  4. 对于规则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": []
    }
    

    有用链接:https://loopback.io/doc/en/lb2/Controlling-data-access.html#applying-access-control-rules