使用Ionic3中的Pubnub进行一对一聊天的频道创建

时间:2018-01-04 09:49:16

标签: angular ionic2 chat pubnub

我已经使用pubnub在Ionic3中实现了聊天。它适用于群聊。用于群聊的频道是“group1-ch”。我在这里添加了我的代码。我在开发一对一聊天方面遇到了一些困难。

PubnubService

import {Injectable, EventEmitter} from '@angular/core';
declare var PUBNUB;
type OnMessageFn = (message, envelope, channelOrGroup, time, channel) => void;

export enum PubNubEventType {
    MESSAGE,
    CONNECT,
    DISCONNECT,
    RECONNECT,
    PUBLISHED,
    HISTORY,
    PRESENCE
}

export class PubNubEvent {
    constructor(public type: PubNubEventType, channel:string, public value: any) {}
}

@Injectable()
export class PubNubService {

    pubnub:any;

    /**
     * Call this method after platform becomes to be ready
     */
    // init() {
    constructor() {

    }
    connectionuuid(uuid){
     this.pubnub = PUBNUB({

            subscribe_key: '*********************************',
            publish_key:   '*********************************',
            uuid:uuid,
            ssl: true,
            secret_key: '**************************************'

        });
    }

    subscribe(channel:string):EventEmitter<PubNubEvent> {
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.subscribe({
            channel : channel,
            withPresence: true,
            presence : (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.PRESENCE, channel, message));
            },
            message : (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.MESSAGE, channel, message));
            },
            connect: (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.CONNECT, channel, message));
            },
            disconnect: (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.DISCONNECT, channel, message));
            },
            reconnect: (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.RECONNECT, channel, message));
            },
            error: (error) => {
                eventEmitter.error(error);
            }, 
        });
        return eventEmitter;
    }

    publish(channel:string, message:any, store_in_history:boolean = true):EventEmitter<PubNubEvent> {
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.publish({
            channel: channel, 
            message: message, 
            store_in_history: store_in_history, 
            callback : (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.PUBLISHED, channel, message));
            },
            // Executes on a publish error.
            error: (error) => {
                eventEmitter.error(error);
            }
        });
        return eventEmitter;
    }

    individual_chat(channel:string, message:any, store_in_history:boolean = true):EventEmitter<PubNubEvent> {
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.publish({
            channel: channel,        
            message: message, 
            store_in_history: store_in_history, 
            callback : (message) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.PUBLISHED, channel, message));
            },
            // Executes on a publish error.
            error: (error) => {
                eventEmitter.error(error);
            }
        });
        return eventEmitter;
    }

    history(channel: string, count:number = 100, start:number = null, end:number = null, reverse:boolean = true, include_token: boolean = true):EventEmitter<PubNubEvent> {
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.history({
            channel: channel,
            callback: (messages) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.HISTORY, channel, messages));
            },
            error: (error) => {
                eventEmitter.error(error);
            },
            count: count,
            start: start,
            end: end,
            reverse: reverse,
            include_token: include_token
        });
        return eventEmitter;
    }

    here_now(channel: string, count:number = 100, start:number = null, end:number = null, reverse:boolean = true, include_token: boolean = true):EventEmitter<PubNubEvent>{
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.here_now({
        channel: channel,
        callback: (m) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.PRESENCE, channel, m));
            },
            error: (error) => {
                eventEmitter.error(error);
            },
            count: count,
            start: start,
            end: end,
            reverse: reverse,
            include_token: include_token
       });
        return eventEmitter;
    }
    where_now(channel: string, count:number = 100, start:number = null, end:number = null, reverse:boolean = true, include_token: boolean = true):EventEmitter<PubNubEvent>{
        let eventEmitter:EventEmitter<PubNubEvent> = new EventEmitter<PubNubEvent>();
        this.pubnub.where_now({
        uuid: channel,
        callback: (m) => {
                eventEmitter.emit(new PubNubEvent(PubNubEventType.PRESENCE, channel, m));
            },
            error: (error) => {
                eventEmitter.error(error);
            },
            count: count,
            start: start,
            end: end,
            reverse: reverse,
            include_token: include_token
       });
        return eventEmitter;
    }
}

我的要求

  1. 有人可以告诉我如何为一对一聊天创建“频道”吗?
  2. 接收方如何知道发件人正在使用哪个频道?
  3. 谢谢和问候

    Anand Raj

1 个答案:

答案 0 :(得分:2)

高级聊天邀请设计模式

让我使用更具体的名字:Anand,Craig,John。每个用户都应订阅入站频道,他们会收到有关邀请的通知:

  • Anand订阅inbound.anand
  • Craig订阅inbound.craig
  • John订阅了inbound.john

现在,如果您知道要联系的用户名,现在可以随时知道要向另一个入站频道发送(发布)邀请的频道。您可以发送消息邀请他们聊天,并将频道作为单独的值提供:

{
    "message": "Hi, this is Anand. I would like to invite you to chat with me.",
    "chat_channel": "abc123",
    "sender_id": "anand"
}

这只是一个简单的示例消息,可能更复杂但重点是,您想要向您要邀请的人发送消息并提供频道名称(它可以是随机生成的字符串或类似于andand_craig ,只要它是使用唯一用户名的Anand和Craig的唯一通道名称。)

您的用户界面将为受邀用户提供一种方式,可以单击接受拒绝按钮来响应邀请。

  • 如果单击接受,则受邀用户会将消息发布回inbox.anand或提供的chat_channel,其中 anand 已经订阅,然后订阅提供的chat_channel,开始与 anand 聊天。

这是一种相当简化的设计模式,但应该提供一些有关如何在两个人甚至一群人之间发起对话的见解。

有关推荐设计模式的更多详细信息:

您还可以查看使用内置聊天功能的PubNub's ChatEngine framework。对于Angular,请参阅Simple Angular ChatEngine example