|
|
@@ -1,29 +1,33 @@
|
|
1
|
1
|
import PubNub from 'pubnub'
|
|
2
|
2
|
|
|
3
|
|
-/**
|
|
|
3
|
+/**
|
|
4
|
4
|
* Provider method holder
|
|
5
|
5
|
* We always reference this object so
|
|
6
|
6
|
* we don't have to hardcode provider specific
|
|
7
|
7
|
* methods for doing chat things.
|
|
8
|
|
- *
|
|
|
8
|
+ *
|
|
9
|
9
|
* This gets overloaded later in the program
|
|
10
|
10
|
*/
|
|
11
|
11
|
const providerMethods = {
|
|
12
|
12
|
publish: () => console.error('no provider publish method set'),
|
|
13
|
13
|
subscribe: () => console.error('no provider subscribe method set'),
|
|
14
|
|
- listen: () => console.error('no provider listen method set')
|
|
|
14
|
+ listen: () => console.error('no provider listen method set'),
|
|
15
|
15
|
}
|
|
16
|
16
|
|
|
17
|
|
-/**
|
|
|
17
|
+/**
|
|
18
|
18
|
* Breaking out as much pubnub specific flavor
|
|
19
|
19
|
*/
|
|
20
|
20
|
const setupPubnub = async uuid => {
|
|
21
|
|
- if(!uuid) return console.error('no pubnub uuid set')
|
|
22
|
|
-
|
|
|
21
|
+ const publishKey = 'pub-c-73f35484-396f-47ff-b4b6-45bed079fd3b'
|
|
|
22
|
+ const subscribeKey = 'sub-c-6cb7f5d0-94e2-11ec-b249-a68c05a281ab'
|
|
|
23
|
+ // console.log('publishKey: ' , publishKey)
|
|
|
24
|
+ if (!uuid) return console.error('no pubnub uuid set')
|
|
|
25
|
+
|
|
23
|
26
|
const pubnubClient = await new PubNub({
|
|
24
|
|
- publishKey: import.meta.env.VITE_PUBNUB_PUBLISH_KEY,
|
|
25
|
|
- subscribeKey: import.meta.env.VITE_PUBNUB_SUBSCRIBE_KEY,
|
|
26
|
|
- uuid
|
|
|
27
|
+ publishKey: publishKey,
|
|
|
28
|
+ subscribeKey: subscribeKey,
|
|
|
29
|
+ logVerbosity: false,
|
|
|
30
|
+ uuid,
|
|
27
|
31
|
})
|
|
28
|
32
|
// Pass pubnub specific methods to our placeholder obj
|
|
29
|
33
|
providerMethods['publish'] = pubnubClient.publish
|
|
|
@@ -33,19 +37,17 @@ const setupPubnub = async uuid => {
|
|
33
|
37
|
return pubnubClient
|
|
34
|
38
|
}
|
|
35
|
39
|
|
|
36
|
|
-
|
|
37
|
40
|
class ChatMessage {
|
|
38
|
41
|
constructor({ title, description }) {
|
|
39
|
|
- this.title = title,
|
|
40
|
|
- this.description = description
|
|
|
42
|
+ ;(this.title = title), (this.description = description)
|
|
41
|
43
|
}
|
|
42
|
44
|
}
|
|
43
|
45
|
|
|
44
|
46
|
const testMessage = new ChatMessage({
|
|
45
|
|
- title: "testing",
|
|
46
|
|
- description: "hello world!",
|
|
|
47
|
+ title: 'testing',
|
|
|
48
|
+ description: 'hello world!',
|
|
47
|
49
|
})
|
|
48
|
|
-const MAIN_CHANNEL = 'hello_world'
|
|
|
50
|
+const MAIN_CHANNEL = 'Channel-Barcelona'
|
|
49
|
51
|
|
|
50
|
52
|
/** Singleton that holds all our chat information */
|
|
51
|
53
|
class Chatter {
|
|
|
@@ -64,14 +66,14 @@ class Chatter {
|
|
64
|
66
|
this.uuid = null
|
|
65
|
67
|
|
|
66
|
68
|
// Setup the main channel
|
|
67
|
|
- this.subscriptions = [MAIN_CHANNEL]
|
|
|
69
|
+ this.subscriptions = [MAIN_CHANNEL, 'Channel-LosAngeles']
|
|
68
|
70
|
this.listeners = {
|
|
69
|
71
|
status: async e => {
|
|
70
|
|
- if (e.category !== "PNConnectedCategory") return
|
|
71
|
72
|
await this.publish(this.subscriptions[0], testMessage)
|
|
|
73
|
+ if (e.category !== 'PNConnectedCategory') return
|
|
72
|
74
|
},
|
|
73
|
75
|
message: this._onMessage,
|
|
74
|
|
- presence: this._onPresence
|
|
|
76
|
+ presence: this._onPresence,
|
|
75
|
77
|
}
|
|
76
|
78
|
}
|
|
77
|
79
|
/**
|
|
|
@@ -79,13 +81,18 @@ class Chatter {
|
|
79
|
81
|
* @param {event} e
|
|
80
|
82
|
*/
|
|
81
|
83
|
async _onMessage(e) {
|
|
82
|
|
- console.log(`received message: ${e.message.title} - ${e.message.description}`)
|
|
|
84
|
+ if (e.message) {
|
|
|
85
|
+ console.log(
|
|
|
86
|
+ `received message: ${e.message.title} - ${e.message.description}`,
|
|
|
87
|
+ e,
|
|
|
88
|
+ )
|
|
|
89
|
+ }
|
|
83
|
90
|
}
|
|
84
|
91
|
async _onPresence(e) {
|
|
85
|
92
|
return
|
|
86
|
93
|
}
|
|
87
|
94
|
async setup(uuid) {
|
|
88
|
|
- this.uuid = uuid
|
|
|
95
|
+ this.uuid = `${uuid}`
|
|
89
|
96
|
this.provider = await setupPubnub(this.uuid)
|
|
90
|
97
|
|
|
91
|
98
|
this._listenFor({ listeners: this.listeners })
|
|
|
@@ -102,21 +109,21 @@ class Chatter {
|
|
102
|
109
|
async publish(channel, message) {
|
|
103
|
110
|
return await providerMethods['publish']({ channel, message })
|
|
104
|
111
|
}
|
|
105
|
|
- /**
|
|
|
112
|
+ /**
|
|
106
|
113
|
* Subscribe to a channels
|
|
107
|
114
|
* Facade so we can hide provider specific methods
|
|
108
|
115
|
* @param {array} channels
|
|
109
|
|
- */
|
|
|
116
|
+ */
|
|
110
|
117
|
_subscribe(channels) {
|
|
111
|
118
|
providerMethods['subscribe']({ channels })
|
|
112
|
119
|
}
|
|
113
|
|
- /**
|
|
|
120
|
+ /**
|
|
114
|
121
|
* Listen to events and set callbacks
|
|
115
|
122
|
* Facade so we can hide provider specific methods
|
|
116
|
|
- */
|
|
|
123
|
+ */
|
|
117
|
124
|
_listenFor({ listeners }) {
|
|
118
|
125
|
providerMethods['listen'](listeners)
|
|
119
|
126
|
}
|
|
120
|
127
|
}
|
|
121
|
128
|
|
|
122
|
|
-export { Chatter }
|
|
|
129
|
+export { Chatter }
|