Refactor WebSocket handling and remove nuxt-visitors module

Replaced nuxt-visitors module with a custom WebSocket implementation for location tracking. Removed redundant code and comments in `useVisitors` composable and introduced the `location.server` plugin to manage user location. Updated dependencies and configurations to reflect these changes, streamlining the approach.
This commit is contained in:
2025-02-02 19:03:49 +01:00
parent 523a4a0582
commit 6ab4ffc092
10 changed files with 53 additions and 101 deletions

View File

@@ -1,17 +1,20 @@
import type { Peer } from 'crossws'
import { defineWebSocketHandler } from 'h3'
import { getQuery } from 'ufo'
export default defineWebSocketHandler({
open(peer) {
open(peer: Peer) {
const locations = Array.from(peer.peers.values()).map(peer => getQuery(peer.websocket.url!))
peer.subscribe('visitors')
peer.publish('visitors', JSON.stringify(locations))
peer.subscribe('nuxt-visitors')
peer.publish('nuxt-visitors', JSON.stringify(locations))
peer.send(JSON.stringify(locations))
},
close(peer) {
peer.unsubscribe('visitors')
close(peer: Peer) {
peer.unsubscribe('nuxt-visitors')
setTimeout(() => {
const locations = Array.from(peer.peers.values()).map(peer => getQuery(peer.websocket.url!))
peer.publish('visitors', JSON.stringify(locations))
peer.publish('nuxt-visitors', JSON.stringify(locations))
}, 500)
},
})