|
|
@@ -47,24 +47,37 @@ const testMessage = new ChatMessage({
|
|
47
|
47
|
})
|
|
48
|
48
|
const MAIN_CHANNEL = 'hello_world'
|
|
49
|
49
|
|
|
|
50
|
+/** Singleton that holds all our chat information */
|
|
50
|
51
|
class Chatter {
|
|
|
52
|
+ /**
|
|
|
53
|
+ * Create our chatter instance
|
|
|
54
|
+ * @return {Chatter} our chatter instance object
|
|
|
55
|
+ */
|
|
51
|
56
|
constructor() {
|
|
|
57
|
+ // Map of each active chat
|
|
52
|
58
|
this.groupings = {}
|
|
|
59
|
+
|
|
|
60
|
+ // Our pubnub instance
|
|
53
|
61
|
this.provider = null
|
|
|
62
|
+
|
|
|
63
|
+ // UUID used to identify unique users
|
|
54
|
64
|
this.uuid = null
|
|
55
|
65
|
|
|
56
|
66
|
// Setup the main channel
|
|
57
|
67
|
this.subscriptions = [MAIN_CHANNEL]
|
|
58
|
|
-
|
|
59
|
68
|
this.listeners = {
|
|
60
|
69
|
status: async e => {
|
|
61
|
70
|
if (e.category !== "PNConnectedCategory") return
|
|
62
|
|
- await this._publish(this.subscriptions[0], testMessage)
|
|
|
71
|
+ await this.publish(this.subscriptions[0], testMessage)
|
|
63
|
72
|
},
|
|
64
|
73
|
message: this._onMessage,
|
|
65
|
74
|
presence: this._onPresence
|
|
66
|
75
|
}
|
|
67
|
76
|
}
|
|
|
77
|
+ /**
|
|
|
78
|
+ * Callback that fires on every message
|
|
|
79
|
+ * @param {event} e
|
|
|
80
|
+ */
|
|
68
|
81
|
async _onMessage(e) {
|
|
69
|
82
|
console.log(e.message.title)
|
|
70
|
83
|
console.log(e.message.description)
|
|
|
@@ -79,12 +92,29 @@ class Chatter {
|
|
79
|
92
|
this._listenFor({ listeners: this.listeners })
|
|
80
|
93
|
this._subscribe(this.subscriptions)
|
|
81
|
94
|
}
|
|
82
|
|
- async _publish(channel, message) {
|
|
|
95
|
+ /**
|
|
|
96
|
+ * Send a message to a channel
|
|
|
97
|
+ * example = new ChatMessage({ title: 'example', description: 'ni' })
|
|
|
98
|
+ * Facade so we can hide provider specific methods
|
|
|
99
|
+ * @param {string} channel
|
|
|
100
|
+ * @param {ChatMessage} message
|
|
|
101
|
+ * @return {object} timestamp
|
|
|
102
|
+ */
|
|
|
103
|
+ async publish(channel, message) {
|
|
83
|
104
|
return await providerMethods['publish']({ channel, message })
|
|
84
|
105
|
}
|
|
|
106
|
+ /**
|
|
|
107
|
+ * Subscribe to a channels
|
|
|
108
|
+ * Facade so we can hide provider specific methods
|
|
|
109
|
+ * @param {array} channels
|
|
|
110
|
+ */
|
|
85
|
111
|
_subscribe(channels) {
|
|
86
|
112
|
providerMethods['subscribe']({ channels })
|
|
87
|
113
|
}
|
|
|
114
|
+ /**
|
|
|
115
|
+ * Listen to events and set callbacks
|
|
|
116
|
+ * Facade so we can hide provider specific methods
|
|
|
117
|
+ */
|
|
88
|
118
|
_listenFor({ listeners }) {
|
|
89
|
119
|
providerMethods['listen'](listeners)
|
|
90
|
120
|
}
|