// 消息类型
import 'dart:convert';

class VpnMessageType {
  VpnMessageType._();
  static const int login = 1;
  static const int heartbeat = 2;
  static const int stateSync = 3;
}

// VPN 消息
class VpnMessage {
  final int type;
  final Map<String, dynamic>? data;

  VpnMessage(this.type, this.data);

  factory VpnMessage.create(int type, Map<String, dynamic>? data) {
    return VpnMessage(type, data);
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'msg_type': type,
      'data': data,
    };
  }

  factory VpnMessage.fromMap(Map<String, dynamic> map) {
    return VpnMessage(
      map['msg_type'] ?? 0,
      map['data'],
    );
  }

  String toJson() => json.encode(toMap());

  factory VpnMessage.fromJson(String source) {
    return VpnMessage.fromMap(json.decode(source) as Map<String, dynamic>);
  }
}
