package com.ard.sample.component.websocket.enums; public enum MessageType { STATE("state"), STATUS("status"), CONTROL("control"), PROGRESS("progress"), LIVESTREAM("livestream"), PING("ping"), // 新增心跳请求 PONG("pong"), // 新增心跳响应 ALL("all"); private final String type; MessageType(String type) { this.type = type; } public String getType() { return type; } public static boolean fromString(String type) { // 遍历MessageType枚举的所有值 for (MessageType messageType : MessageType.values()) { // 如果当前枚举值的type属性与传入的type字符串忽略大小写相等 if (messageType.type.equalsIgnoreCase(type)) { // 返回当前枚举值 //return messageType; return true; } } // 如果遍历完所有枚举值都没有找到匹配的,则抛出异常 //throw new IllegalArgumentException("Unknown message type: " + type); return false; } }