⚡ App > Stop listening to socket events
This commit is contained in:
+36
-15
@@ -30,8 +30,9 @@ class Chat
|
||||
// Create a channel for this specific chat
|
||||
this.chatSocket = this.config.socket.of('/project/' + this.slug + '/chat')
|
||||
|
||||
// Connection event
|
||||
this.chatSocket.on('connection', (socket) =>
|
||||
// Callbacks
|
||||
this.socketCallbacks = {}
|
||||
this.socketCallbacks.connection = (socket) =>
|
||||
{
|
||||
// Create user
|
||||
const user = this.createUser()
|
||||
@@ -49,8 +50,10 @@ class Chat
|
||||
broadcastUser.color = user.color
|
||||
socket.emit('user', broadcastUser)
|
||||
|
||||
// On message
|
||||
socket.on('message', (data) =>
|
||||
|
||||
const userSocketCallbacks = {}
|
||||
|
||||
userSocketCallbacks.message = (data) =>
|
||||
{
|
||||
// Add ID and save
|
||||
data.id = ids.getId()
|
||||
@@ -74,10 +77,9 @@ class Chat
|
||||
{
|
||||
console.log(`${chalk.green.bold('chat > socket')} - ${chalk.cyan('message')} - ${chalk.cyan(user.name)} - ${chalk.cyan(data.text)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// On update user
|
||||
socket.on('update_user', (data) =>
|
||||
userSocketCallbacks.updateUser = (data) =>
|
||||
{
|
||||
const newName = data.name.trim()
|
||||
const oldName = user.name
|
||||
@@ -96,10 +98,9 @@ class Chat
|
||||
{
|
||||
console.log(`${chalk.green.bold('chat > socket')} - ${chalk.cyan('update_user')} - ${chalk.cyan(oldName + ' > ' + user.name)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// On alert
|
||||
socket.on('alert', () =>
|
||||
userSocketCallbacks.alert = () =>
|
||||
{
|
||||
// Transfer to the watcher socket
|
||||
this.watcherSocket.emit('alert')
|
||||
@@ -109,14 +110,28 @@ class Chat
|
||||
{
|
||||
console.log(`${chalk.green.bold('chat > socket')} - ${chalk.cyan('alert')} - ${chalk.cyan(user.name)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// On disconnect
|
||||
socket.on('disconnect', () =>
|
||||
userSocketCallbacks.disconnect = () =>
|
||||
{
|
||||
// Stop listening
|
||||
socket.off('message', userSocketCallbacks.message)
|
||||
socket.off('update_user', userSocketCallbacks.updateUser)
|
||||
socket.off('alert', userSocketCallbacks.alert)
|
||||
socket.off('disconnect', userSocketCallbacks.disconnect)
|
||||
|
||||
console.log(`${chalk.green.bold('chat > socket')} - ${chalk.cyan('disconnect')} - ${chalk.cyan(user.name)}`)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// On message
|
||||
socket.on('message', userSocketCallbacks.message)
|
||||
socket.on('update_user', userSocketCallbacks.updateUser)
|
||||
socket.on('alert', userSocketCallbacks.alert)
|
||||
socket.on('disconnect', userSocketCallbacks.disconnect)
|
||||
}
|
||||
|
||||
// Connection event
|
||||
this.chatSocket.on('connection', this.socketCallbacks.connection)
|
||||
}
|
||||
|
||||
createUser()
|
||||
@@ -261,6 +276,12 @@ class Chat
|
||||
this.chatSocket.emit('message', broadcastMessage)
|
||||
}, 1500)
|
||||
}
|
||||
|
||||
destructor()
|
||||
{
|
||||
// Stop listening
|
||||
this.chatSocket.off('connection', this.socketCallbacks.connection)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Chat
|
||||
|
||||
@@ -141,6 +141,11 @@ class Files
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
destructor()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Files
|
||||
|
||||
+28
-15
@@ -223,8 +223,10 @@ class App
|
||||
console.log(`${chalk.green.bold('app > socket')} - ${chalk.cyan('connect')} - ${chalk.cyan(socket.id)}`)
|
||||
}
|
||||
|
||||
// Start project
|
||||
socket.on('start_project', (data) =>
|
||||
// Callbacks
|
||||
const callbacks = {}
|
||||
|
||||
callbacks.startProject = (data) =>
|
||||
{
|
||||
data.watcherSocket = socket // Add watcher socket
|
||||
project = this.projects.createProject(data)
|
||||
@@ -242,10 +244,9 @@ class App
|
||||
}
|
||||
|
||||
socket.emit('started')
|
||||
})
|
||||
}
|
||||
|
||||
// Update file
|
||||
socket.on('update_file', (data) =>
|
||||
callbacks.updateFile = (data) =>
|
||||
{
|
||||
project.files.createVersion(data.path, data.content)
|
||||
project.zipNeedsUpdate = true
|
||||
@@ -259,10 +260,9 @@ class App
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Create file
|
||||
socket.on('create_file', (data) =>
|
||||
callbacks.createFile = (data) =>
|
||||
{
|
||||
project.files.create(data.path, data.content)
|
||||
project.zipNeedsUpdate = true
|
||||
@@ -276,10 +276,9 @@ class App
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Delete file
|
||||
socket.on('delete_file', (data) =>
|
||||
callbacks.deleteFile = (data) =>
|
||||
{
|
||||
project.files.delete(data.path)
|
||||
project.zipNeedsUpdate = true
|
||||
@@ -293,19 +292,33 @@ class App
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Disconnect
|
||||
socket.on('disconnect', () =>
|
||||
callbacks.disconnect = () =>
|
||||
{
|
||||
// Delete project
|
||||
this.projects.deleteProject(project.slug)
|
||||
|
||||
// Stop listening
|
||||
socket.off('start_project', callbacks.startProject)
|
||||
socket.off('update_file', callbacks.updateFile)
|
||||
socket.off('create_file', callbacks.createFile)
|
||||
socket.off('delete_file', callbacks.disconnect)
|
||||
socket.off('disconnect', callbacks.disconnect)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug >= 1)
|
||||
{
|
||||
console.log(`${chalk.green.bold('app > socket')} - ${chalk.cyan('disconnect')} - ${chalk.cyan(socket.id)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Start listening
|
||||
socket.on('start_project', callbacks.startProject)
|
||||
socket.on('update_file', callbacks.updateFile)
|
||||
socket.on('create_file', callbacks.createFile)
|
||||
socket.on('delete_file', callbacks.deleteFile)
|
||||
socket.on('disconnect', callbacks.disconnect)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+15
-3
@@ -43,8 +43,9 @@ class Project
|
||||
// Create a channel for this specific project
|
||||
this.projectSocket = this.config.socket.of('/project/' + this.slug)
|
||||
|
||||
// Connection event
|
||||
this.projectSocket.on('connection', (socket) =>
|
||||
// Callbacks
|
||||
this.socketCallbacks = {}
|
||||
this.socketCallbacks.connection = (socket) =>
|
||||
{
|
||||
this.projectSocket.emit('update_project', this.describe())
|
||||
|
||||
@@ -53,7 +54,10 @@ class Project
|
||||
{
|
||||
console.log(`${chalk.green.bold('project > socket')} - ${chalk.cyan('connection')} - ${chalk.cyan(socket.id)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Connection event
|
||||
this.projectSocket.on('connection', this.socketCallbacks.connection)
|
||||
}
|
||||
|
||||
getZipBuffer()
|
||||
@@ -306,6 +310,14 @@ class ClassName extends AnotherClass
|
||||
|
||||
destructor()
|
||||
{
|
||||
// Destroy chat and files
|
||||
this.files.destructor()
|
||||
this.chat.destructor()
|
||||
|
||||
// Stop listening
|
||||
this.projectSocket.off('connection', this.socketCallbacks.connection)
|
||||
|
||||
// Emit event
|
||||
this.projectSocket.emit('destruct')
|
||||
}
|
||||
}
|
||||
|
||||
+7
-3
@@ -16,8 +16,9 @@ class Projects
|
||||
// Save original socket and create a channel for projects
|
||||
this.projectsSocket = this.config.socket.of('/projects')
|
||||
|
||||
// Connection event
|
||||
this.projectsSocket.on('connection', (socket) =>
|
||||
// Callbacks
|
||||
this.socketCallbacks = {}
|
||||
this.socketCallbacks.connection = (socket) =>
|
||||
{
|
||||
// // Send the config to the new socket
|
||||
socket.emit('config', { domain: this.config.domain, server: this.config.server })
|
||||
@@ -29,7 +30,10 @@ class Projects
|
||||
{
|
||||
console.log(`${chalk.green.bold('projects > socket')} - ${chalk.cyan('connection')} - ${chalk.cyan(socket.id)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Connection event
|
||||
this.projectsSocket.on('connection', this.socketCallbacks.connection)
|
||||
}
|
||||
|
||||
createProject(_options)
|
||||
|
||||
Reference in New Issue
Block a user