wangrong
2022-07-08 7e9badac4f3df3650e72bd419f9cdaa485b054d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Config, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { CoolSocket, SocketEnvent } from '@cool-midway/socket';
import { Socket } from 'socket.io';
// import { ChatUserEntity } from '../entity/user';
import { Repository } from 'typeorm';
 
import * as jwt from 'jsonwebtoken';
 
/**
 * socket的事件
 */
@Provide()
@Scope(ScopeEnum.Singleton)
@CoolSocket() // 支持设置命名空间 如:@CoolSocket('/test') 
export class SocketHandler {
  // 获得模块的配置信息
  @Config('module.chat.jwt')
  jwtConfig;
 
  public clients: Map<String, Socket>;
 
//   @InjectEntityModel(ChatUserEntity)
//   chatUserEntity: Repository<ChatUserEntity>;
 
  /**
   * 连接成功
   * @param data
   */
  @SocketEnvent()
  async connection(socket: Socket) {
    // console.log('socket事件', socket.id, socket.handshake.query);
    // console.log('socket事件', socket.id, socket.handshake);
    console.log(socket);
    this.clients.set(socket.id, socket)
    const { token } = socket.handshake.query;
    console.log('-----------------------------------------------------------------------------------------------------------------------')
    console.log(token)
    // let tokenInfo;
    // try {
    //   // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    //   // @ts-ignore
    //   tokenInfo = jwt.verify(token, this.jwtConfig.secret);
    // } catch (error) {
    //   // 如果验证不通过则断开连接
    //   socket.emit('comm', '校验失效');
    //   socket.disconnect();
    //   return;
    // }
    socket.on('disconnect', eventSocket => {
      this.clients.delete(socket.id)
      console.log('断开连接');
    });
 
 
    socket.on('msg', msg => {
      console.log('客户端消息', msg);
      socket.emit('data', 're:'+msg.op +" "+ msg.deviceid)
 
    });
 
    socket.on('devicelog',msg => {
      //解析终端上报的状态消息
      
      //入库
      
      //推送到前端
      socket.broadcast.emit('');
    });
 
  }
 
  /**
   * 连接成功
   * @param data
   */
   @SocketEnvent()
   async broadcast(socket: Socket) {
       
       //推送到前端
       socket.broadcast.emit('');
 
   }
 
}