|
|
@@ -1,11 +1,13 @@
|
|
|
import Logger from '@ioc:Adonis/Core/Logger'
|
|
|
-import { Server, Socket } from 'socket.io'
|
|
|
+import { Namespace, Server, Socket } from 'socket.io'
|
|
|
import AdonisServer from '@ioc:Adonis/Core/Server'
|
|
|
import auth from 'App/Middleware/Auth'
|
|
|
class Ws {
|
|
|
public io: Server
|
|
|
+ public clientsIO: Namespace
|
|
|
+ public adminIO: Namespace
|
|
|
private booted = false
|
|
|
- private clients: any[] = []
|
|
|
+ private clients: { [key: string]: any } = {}
|
|
|
public boot() {
|
|
|
/**
|
|
|
* Ignore multiple calls to the boot method
|
|
|
@@ -16,34 +18,58 @@ class Ws {
|
|
|
|
|
|
this.booted = true
|
|
|
this.io = new Server(AdonisServer.instance!, {
|
|
|
+ path: '/ws',
|
|
|
cors: {
|
|
|
origin: '*'
|
|
|
}
|
|
|
})
|
|
|
+ this.clientsIO = this.io.of('client')
|
|
|
+ this.adminIO = this.io.of('admin')
|
|
|
|
|
|
- this.io.on('connection', (socket: Socket) => {
|
|
|
- this.clients.push({ id: socket.id })
|
|
|
- socket.on('clients', () => {
|
|
|
- socket.emit('clients', this.clients)
|
|
|
+ this.clientsIO.on('connection', (socket: Socket) => {
|
|
|
+ Logger.info('Client connected ' + JSON.stringify(socket.handshake))
|
|
|
+ this.clients[socket.id] = {
|
|
|
+ ...socket.handshake
|
|
|
+ }
|
|
|
+
|
|
|
+ socket.on('disconnect', (reason) => {
|
|
|
+ Logger.info('Client disconnected ' + reason)
|
|
|
+ delete this.clients[socket.id]
|
|
|
})
|
|
|
|
|
|
- socket.on('sms', ({ id }) => {
|
|
|
- this.io.to(id).emit('sms', { message: 'Hello' })
|
|
|
+ socket.on('info', (data) => {
|
|
|
+ this.clients[socket.id] = { id: socket.id, ...this.clients[socket.id], ...data }
|
|
|
})
|
|
|
|
|
|
- console.log(
|
|
|
- 'Client connected ' + JSON.stringify(socket.handshake.query) + ' id=' + socket.id
|
|
|
- )
|
|
|
- socket.on('disconnect', (reason) => {
|
|
|
- console.log(
|
|
|
- 'Client disconnected ' +
|
|
|
- JSON.stringify(socket.handshake.query) +
|
|
|
- ' id=' +
|
|
|
- socket.id
|
|
|
- )
|
|
|
+ socket.on('result', (args: { to: string; action: string; data: any }) => {
|
|
|
+ Logger.info('got cmdResult ' + JSON.stringify(args))
|
|
|
+ this.adminIO.to(args.to).emit('result', {
|
|
|
+ from: socket.id,
|
|
|
+ action: args.action,
|
|
|
+ data: args.data
|
|
|
+ })
|
|
|
})
|
|
|
- socket.on('info', (data) => {
|
|
|
- console.log(data)
|
|
|
+ })
|
|
|
+
|
|
|
+ this.adminIO.on('connection', (socket: Socket) => {
|
|
|
+ Logger.info('Admin connected ' + JSON.stringify(socket.handshake))
|
|
|
+ socket.on('clients', (args) => {
|
|
|
+ this.adminIO.to(socket.id).emit('clients', Object.values(this.clients))
|
|
|
+ })
|
|
|
+ socket.on('sendCmd', (args: { to: string; action: string; data: any }) => {
|
|
|
+ Logger.info(
|
|
|
+ 'sent cmd ' +
|
|
|
+ JSON.stringify({
|
|
|
+ from: socket.id,
|
|
|
+ action: args.action,
|
|
|
+ data: args.data
|
|
|
+ })
|
|
|
+ )
|
|
|
+ this.clientsIO.to(args.to).emit('cmd', {
|
|
|
+ from: socket.id,
|
|
|
+ action: args.action,
|
|
|
+ data: args.data
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
}
|