ntminer 5 anos atrás
pai
commit
7254e86bd6
1 arquivos alterados com 9 adições e 6 exclusões
  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);
         }
     }
 }