ntminer 5 years ago
parent
commit
7252d1b887

+ 4 - 1
src/WsServer/Core/ISession.cs

@@ -1,5 +1,7 @@
 using NTMiner.User;
+using NTMiner.Ws;
 using System;
+using WebSocketSharp;
 using WebSocketSharp.Server;
 
 namespace NTMiner.Core {
@@ -24,7 +26,8 @@ namespace NTMiner.Core {
         /// </summary>
         DateTime ActiveOn { get; }
         string WsSessionId { get; }
-        bool TryGetWsSession(out IWebSocketSession wsSession);
+        void CloseAsync(CloseStatusCode code, string reason);
+        void SendAsync(WsMessage message, string password);
         void Active();
     }
 }

+ 20 - 1
src/WsServer/Core/Impl/AbstractSession.cs

@@ -1,5 +1,7 @@
 using NTMiner.User;
+using NTMiner.Ws;
 using System;
+using WebSocketSharp;
 using WebSocketSharp.Server;
 
 namespace NTMiner.Core.Impl {
@@ -32,7 +34,24 @@ namespace NTMiner.Core.Impl {
 
         public string WsSessionId { get; private set; }
 
-        public bool TryGetWsSession(out IWebSocketSession wsSession) {
+        public void CloseAsync(CloseStatusCode code, string reason) {
+            if (TryGetWsSession(out IWebSocketSession wsSession)) {
+                wsSession.Context.WebSocket.CloseAsync(code, reason);
+            }
+        }
+
+        public void SendAsync(WsMessage message, string password) {
+            if (TryGetWsSession(out IWebSocketSession wsSession)) {
+                if (WsUserName.IsBinarySupported) {
+                    wsSession.Context.WebSocket.SendAsync(message.SignToBytes(password), completed: null);
+                }
+                else {
+                    wsSession.Context.WebSocket.SendAsync(message.SignToJson(password), completed: null);
+                }
+            }
+        }
+
+        private bool TryGetWsSession(out IWebSocketSession wsSession) {
             return _wsSessionManager.TryGetSession(this.WsSessionId, out wsSession);
         }
 

+ 15 - 24
src/WsServer/Core/Impl/AbstractSessionSet`1.cs

@@ -49,9 +49,7 @@ namespace NTMiner.Core.Impl {
                         toCloses = _dicByWsSessionId.Values.Where(a => a.LoginName == message.LoginName).ToArray();
                     }
                     foreach (var item in toCloses) {
-                        if (item.TryGetWsSession(out IWebSocketSession session)) {
-                            session.Context.WebSocket.CloseAsync(CloseStatusCode.Normal, "用户已被禁用");
-                        }
+                        item.CloseAsync(CloseStatusCode.Normal, "用户已被禁用");
                     }
                 }
             }, this.GetType());
@@ -68,29 +66,22 @@ namespace NTMiner.Core.Impl {
                 needReGetServerAddressSessions = _dicByWsSessionId.Values.Where(a => hash.GetTargetNode(a.ClientId) != thisNodeIp).ToList();
             }
             if (needReGetServerAddressSessions.Count != 0) {
-                if (_isMinerClient) {
-                    foreach (var session in needReGetServerAddressSessions) {
-                        if (session.TryGetWsSession(out IWebSocketSession wsSession)) {
-                            string password = ((IMinerClientSession)session).GetSignPassword();
-                            try {
-                                wsSession.Context.WebSocket.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ReGetServerAddress).SignToJson(password), completed: null);
-                            }
-                            catch {
-                            }
+                foreach (var session in needReGetServerAddressSessions) {
+                    string password = null;
+                    if (_isMinerClient) {
+                        password = ((IMinerClientSession)session).GetSignPassword();
+                    }
+                    else if (_isMinerStudio) {
+                        var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(session.LoginName));
+                        if (userData != null) {
+                            password = userData.Password;
                         }
                     }
-                }
-                else if (_isMinerStudio) {
-                    foreach (var session in needReGetServerAddressSessions) {
-                        if (session.TryGetWsSession(out IWebSocketSession wsSession)) {
-                            var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(session.LoginName));
-                            if (userData != null) {
-                                try {
-                                    wsSession.Context.WebSocket.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ReGetServerAddress).SignToJson(userData.Password), completed: null);
-                                }
-                                catch {
-                                }
-                            }
+                    if (!string.IsNullOrEmpty(password)) {
+                        try {
+                            session.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.ReGetServerAddress), password);
+                        }
+                        catch {
                         }
                     }
                 }

+ 2 - 2
src/WsServer/Core/Impl/MinerClientSessionSet.cs

@@ -245,10 +245,10 @@ namespace NTMiner.Core.Impl {
         }
 
         public void SendToMinerClientAsync(Guid clientId, WsMessage message) {
-            if (TryGetByClientId(clientId, out IMinerClientSession minerClientSession) && minerClientSession.TryGetWsSession(out IWebSocketSession wsSession)) {
+            if (TryGetByClientId(clientId, out IMinerClientSession minerClientSession)) {
                 string password = minerClientSession.GetSignPassword();
                 try {
-                    wsSession.Context.WebSocket.SendAsync(message.SignToJson(password), completed: null);
+                    minerClientSession.SendAsync(message, password);
                 }
                 catch {
                 }

+ 6 - 8
src/WsServer/Core/Impl/MinerStudioSessionSet.cs

@@ -154,14 +154,12 @@ namespace NTMiner.Core.Impl {
         public void SendToMinerStudioAsync(string loginName, WsMessage message) {
             List<IMinerStudioSession> minerStudioSessions = GetSessionsByLoginName(loginName);
             foreach (var minerStudioSession in minerStudioSessions) {
-                if (minerStudioSession.TryGetWsSession(out IWebSocketSession wsSession)) {
-                    var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(minerStudioSession.LoginName));
-                    if (userData != null) {
-                        try {
-                            wsSession.Context.WebSocket.SendAsync(message.SignToJson(userData.Password), completed: null);
-                        }
-                        catch {
-                        }
+                var userData = WsRoot.ReadOnlyUserSet.GetUser(UserId.CreateLoginNameUserId(minerStudioSession.LoginName));
+                if (userData != null) {
+                    try {
+                        minerStudioSession.SendAsync(message, userData.Password);
+                    }
+                    catch {
                     }
                 }
             }