Skip to content

Commit 3a6d858

Browse files
committed
feat: add websocket support for motd
1 parent a5a8e04 commit 3a6d858

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/components/layout/Header.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,15 @@ import BanStore from '@/composables/stores/ban'
269269
import NotificationsStore from '@/composables/stores/notifications'
270270
import humanDate from '@/composables/filters/humanDate'
271271
import { motdApi } from '@/api'
272+
import { watchPublicChannel } from '@/composables/services/websocket'
272273
273274
export default {
274275
components: { Breadcrumbs, LoginModal, InviteModal, RegisterModal, Alert },
275276
setup() {
276277
onBeforeMount(() => {
277-
motdApi.get().then(d => v.motdData = d).catch(() => {})
278+
let fetchMotd = () => motdApi.get().then(d => v.motdData = d).catch(() => {})
279+
fetchMotd()
280+
watchPublicChannel(d => d.action === 'announcement' ? fetchMotd() : null)
278281
})
279282
/* Internal Methods */
280283
const scrollHeader = () => {

src/composables/services/websocket.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const socketcluster = require('socketcluster-client')
88
// Public channel idenitfier and general options
99
let options = { waitForAuth: true }
1010
let userChannel
11+
let publicChannel
1112
let session = reactive({ user: {} })
1213

1314
// Initiate the connection to the websocket server
@@ -39,6 +40,12 @@ export const socketLogout = socketUser => {
3940
socket.emit('loggedOut')
4041
}
4142

43+
export const watchPublicChannel = handler => {
44+
if (window.websocket_logs) console.log('Watching public channel.')
45+
if (publicChannel) publicChannel.watch(handler)
46+
else setTimeout(() => watchPublicChannel(handler), 1000)
47+
}
48+
4249
export const watchUserChannel = handler => {
4350
if (window.websocket_logs) console.log('Watching user channel.')
4451
if (userChannel) userChannel.watch(handler)
@@ -87,7 +94,7 @@ export default {
8794
})
8895
}
8996
else if (JSON.parse(channelName).type === 'public') {
90-
socket.watch(channelName, d => d.action === 'announcement' ? alertStore.warn(d.message) : null)
97+
// Placeholder for future public notifications if necessary
9198
}
9299
else window.websocket_logs ? console.log('Not watching', channelName) : null
93100

@@ -125,14 +132,15 @@ export default {
125132
socket.on('connect', status => status.isAuthenticated ? socket.emit('loggedIn') : null)
126133

127134
// always subscribe to the public channel
128-
socket.subscribe(publicChannelKey, options)
135+
publicChannel = socket.subscribe(publicChannelKey, { waitForAuth: false })
129136

130137
/* Provide Store Data */
131138
return provide(WebsocketService, {
132139
socketLogin,
133140
socketLogout,
134141
watchUserChannel,
135142
unwatchUserChannel,
143+
watchPublicChannel,
136144
isOnline,
137145
})
138146
},

0 commit comments

Comments
 (0)