2022-04-03 14:24:17 -05:00
|
|
|
import { io } from 'socket.io-client'
|
|
|
|
import EventEmitter from 'events'
|
2021-09-01 20:07:11 -05:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
class ServerSocket extends EventEmitter {
|
|
|
|
constructor(store) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.$store = store
|
|
|
|
this.socket = null
|
|
|
|
this.connected = false
|
|
|
|
this.serverAddress = null
|
|
|
|
this.token = null
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:08:27 -05:00
|
|
|
$on(evt, callback) {
|
|
|
|
if (this.socket) this.socket.on(evt, callback)
|
|
|
|
else console.error('$on Socket not initialized')
|
|
|
|
}
|
|
|
|
|
|
|
|
$off(evt, callback) {
|
|
|
|
if (this.socket) this.socket.off(evt, callback)
|
|
|
|
else console.error('$off Socket not initialized')
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
connect(serverAddress, token) {
|
|
|
|
this.serverAddress = serverAddress
|
|
|
|
this.token = token
|
|
|
|
|
|
|
|
console.log('[SOCKET] Connect Socket', this.serverAddress)
|
|
|
|
|
|
|
|
const socketOptions = {
|
|
|
|
transports: ['websocket'],
|
|
|
|
upgrade: false,
|
|
|
|
// reconnectionAttempts: 3
|
|
|
|
}
|
|
|
|
this.socket = io(this.serverAddress, socketOptions)
|
|
|
|
this.setSocketListeners()
|
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
if (this.socket) this.socket.disconnect()
|
2022-07-07 17:24:26 -05:00
|
|
|
this.removeListeners()
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setSocketListeners() {
|
|
|
|
this.socket.on('connect', this.onConnect.bind(this))
|
|
|
|
this.socket.on('disconnect', this.onDisconnect.bind(this))
|
|
|
|
this.socket.on('init', this.onInit.bind(this))
|
2022-04-09 20:29:59 -05:00
|
|
|
this.socket.on('user_updated', this.onUserUpdated.bind(this))
|
2022-04-15 20:48:39 -05:00
|
|
|
this.socket.on('user_item_progress_updated', this.onUserItemProgressUpdated.bind(this))
|
2022-04-03 19:16:17 -05:00
|
|
|
|
2022-04-23 14:38:29 -05:00
|
|
|
// Good for testing socket requests
|
|
|
|
// this.socket.onAny((evt, args) => {
|
|
|
|
// console.log(`[SOCKET] onAny: ${this.socket.id}: ${evt} ${JSON.stringify(args)}`)
|
|
|
|
// })
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:24:26 -05:00
|
|
|
removeListeners() {
|
|
|
|
if (!this.socket) return
|
|
|
|
this.socket.removeAllListeners()
|
|
|
|
if (this.socket.io && this.socket.io.removeAllListeners) {
|
|
|
|
this.socket.io.removeAllListeners()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
onConnect() {
|
|
|
|
console.log('[SOCKET] Socket Connected ' + this.socket.id)
|
|
|
|
this.connected = true
|
|
|
|
this.$store.commit('setSocketConnected', true)
|
|
|
|
this.emit('connection-update', true)
|
2022-04-10 20:31:47 -05:00
|
|
|
this.socket.emit('auth', this.token) // Required to connect a user with their socket
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
onDisconnect(reason) {
|
|
|
|
console.log('[SOCKET] Socket Disconnected: ' + reason)
|
|
|
|
this.connected = false
|
|
|
|
this.$store.commit('setSocketConnected', false)
|
|
|
|
this.emit('connection-update', false)
|
|
|
|
}
|
|
|
|
|
|
|
|
onInit(data) {
|
|
|
|
console.log('[SOCKET] Initial socket data received', data)
|
|
|
|
this.emit('initialized', true)
|
|
|
|
}
|
2022-04-09 20:29:59 -05:00
|
|
|
|
|
|
|
onUserUpdated(data) {
|
|
|
|
console.log('[SOCKET] User updated', data)
|
|
|
|
this.emit('user_updated', data)
|
|
|
|
}
|
2022-04-15 20:48:39 -05:00
|
|
|
|
2023-06-19 12:37:44 -05:00
|
|
|
onUserItemProgressUpdated(payload) {
|
|
|
|
console.log('[SOCKET] User Item Progress Updated', JSON.stringify(payload))
|
|
|
|
this.$store.commit('user/updateUserMediaProgress', payload.data)
|
|
|
|
this.emit('user_media_progress_updated', payload)
|
2022-04-15 20:48:39 -05:00
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ app, store }, inject) => {
|
|
|
|
inject('socket', new ServerSocket(store))
|
2021-09-01 20:07:11 -05:00
|
|
|
}
|