ntminer 5 년 전
부모
커밋
7254e86bd6
1개의 변경된 파일9개의 추가작업 그리고 6개의 파일을 삭제
  1. 9 6
      src/WsServer/MessageEventArgsExtensions.cs

+ 9 - 6
src/WsServer/MessageEventArgsExtensions.cs

@@ -5,16 +5,19 @@ using WebSocketSharp;
 namespace NTMiner {
     public static class MessageEventArgsExtensions {
         public static T ToWsMessage<T>(this MessageEventArgs e) where T : WsMessage {
+            // IsPing、IsBinary、IsText三者是互斥的,经查源码实际上是判断一个Opcode枚举类型的值
+            if (e.IsPing) {
+                throw new InvalidProgramException("Ping消息不应走到这一步");
+            }
             if (e.IsBinary) {
-                if (e.IsPing) {
-                    throw new InvalidProgramException("Ping消息不应走到这一步");
-                }
                 return VirtualRoot.BinarySerializer.Deserialize<T>(e.RawData);
             }
-            if (string.IsNullOrEmpty(e.Data) || e.Data[0] != '{' || e.Data[e.Data.Length - 1] != '}') {
-                return null;
+            else {
+                if (string.IsNullOrEmpty(e.Data) || e.Data[0] != '{' || e.Data[e.Data.Length - 1] != '}') {
+                    return null;
+                }
+                return VirtualRoot.JsonSerializer.Deserialize<T>(e.Data);
             }
-            return VirtualRoot.JsonSerializer.Deserialize<T>(e.Data);
         }
     }
 }