优化部分设置
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net.Security;
 | 
			
		||||
using System.Timers;
 | 
			
		||||
using log4net;
 | 
			
		||||
using WebSocketSharp;
 | 
			
		||||
@@ -52,6 +53,56 @@ namespace WebSocketTool.Client
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #region Proxy
 | 
			
		||||
 | 
			
		||||
        private string proxyAddress;
 | 
			
		||||
        private string proxyUserName;
 | 
			
		||||
        private string proxyPassword;
 | 
			
		||||
        
 | 
			
		||||
        public string ProxyAddress
 | 
			
		||||
        {
 | 
			
		||||
            get => proxyAddress;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                proxyAddress = value;
 | 
			
		||||
                RaisePropertyChanged(nameof(ProxyAddress));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string ProxyUserName
 | 
			
		||||
        {
 | 
			
		||||
            get => proxyUserName;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                proxyUserName = value;
 | 
			
		||||
                RaisePropertyChanged(nameof(ProxyUserName));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string ProxyPassword
 | 
			
		||||
        {
 | 
			
		||||
            get => proxyPassword;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                proxyPassword = value;
 | 
			
		||||
                RaisePropertyChanged(nameof(ProxyPassword));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private bool isProxyChecked;
 | 
			
		||||
 | 
			
		||||
        public bool IsProxyChecked
 | 
			
		||||
        {
 | 
			
		||||
            get => isProxyChecked;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                isProxyChecked = value;
 | 
			
		||||
                RaisePropertyChanged(nameof(IsProxyChecked));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        public void Connect()
 | 
			
		||||
        {
 | 
			
		||||
            if (string.IsNullOrEmpty(WsUrl))
 | 
			
		||||
@@ -72,6 +123,11 @@ namespace WebSocketTool.Client
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Connect Socket");
 | 
			
		||||
            if (IsProxyChecked)
 | 
			
		||||
            {
 | 
			
		||||
                view.AppendInfo($"use proxy: {ProxyAddress}");
 | 
			
		||||
                mClient.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
 | 
			
		||||
            }
 | 
			
		||||
            mClient.ConnectAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -142,7 +198,14 @@ namespace WebSocketTool.Client
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            pingTimer = new Timer {Interval = PingTime, AutoReset = false};
 | 
			
		||||
            if (PingTime <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                view.AppendInfo("ping time interval must > 0");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            pingTimer = new Timer {Interval = PingTime, AutoReset = true};
 | 
			
		||||
            pingTimer.Enabled = true;
 | 
			
		||||
            pingTimer.Elapsed += (s, e) =>
 | 
			
		||||
            {
 | 
			
		||||
                Ping();
 | 
			
		||||
@@ -171,10 +234,32 @@ namespace WebSocketTool.Client
 | 
			
		||||
        {
 | 
			
		||||
            view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n InitSocketClient");
 | 
			
		||||
            mClient = new SocketClient(WsUrl);
 | 
			
		||||
            mClient.SetServerCertificateValidationCallback((sender, certificate, chain, errors) =>
 | 
			
		||||
            {
 | 
			
		||||
                App.RunOnUIThread(() => { view.AppendInfo($"ServerCertificateError:{errors}"); });
 | 
			
		||||
                Log.Info($"error:{errors}");
 | 
			
		||||
                Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}");
 | 
			
		||||
                Log.Info("ChainStatus:");
 | 
			
		||||
                foreach (var status in chain.ChainStatus)
 | 
			
		||||
                {
 | 
			
		||||
                    Log.Info($"{status.StatusInformation}: {status}");
 | 
			
		||||
                }
 | 
			
		||||
                Log.Info("ChainElements:");
 | 
			
		||||
                foreach (var element in chain.ChainElements)
 | 
			
		||||
                {
 | 
			
		||||
                    Log.Info($"element: {element.Certificate},{element.Information}");
 | 
			
		||||
                }
 | 
			
		||||
                return errors == SslPolicyErrors.None;
 | 
			
		||||
            });
 | 
			
		||||
            mClient.OpenEvent += ClientOnOpenEvent;
 | 
			
		||||
            mClient.CloseEvent += ClientOnCloseEvent;
 | 
			
		||||
            mClient.ErrorEvent += ClientOnErrorEvent;
 | 
			
		||||
            mClient.MessageEvent += ClientOnMessageEvent;
 | 
			
		||||
            if (IsProxyChecked)
 | 
			
		||||
            {
 | 
			
		||||
                mClient.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ClientOnMessageEvent(object sender, MessageEventArgs e)
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@
 | 
			
		||||
    <Grid Margin="5">
 | 
			
		||||
        <Grid.RowDefinitions>
 | 
			
		||||
            <RowDefinition Height="36"/>
 | 
			
		||||
            <RowDefinition Height="40"/>
 | 
			
		||||
            <RowDefinition Height="120"/>
 | 
			
		||||
            <RowDefinition Height="*"/>
 | 
			
		||||
        </Grid.RowDefinitions>
 | 
			
		||||
@@ -27,8 +28,31 @@
 | 
			
		||||
            <Button x:Name="DisconnectBtn" Grid.Column="2" Content="断开" Click="DisconnectBtn_OnClick" Width="80" Height="32"
 | 
			
		||||
                HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand"  IsEnabled="{Binding IsCloseEnable, Mode=OneWay}"/>
 | 
			
		||||
        </Grid>
 | 
			
		||||
        
 | 
			
		||||
        <Grid Grid.Row="1" VerticalAlignment="Center" Height="32" Margin="0,4,0,0">
 | 
			
		||||
           <Grid.ColumnDefinitions>
 | 
			
		||||
               <ColumnDefinition Width="Auto" />
 | 
			
		||||
               <ColumnDefinition Width="2*"/>
 | 
			
		||||
               <ColumnDefinition Width="Auto"/>
 | 
			
		||||
               <ColumnDefinition Width="*"/>
 | 
			
		||||
               <ColumnDefinition Width="Auto"/>
 | 
			
		||||
               <ColumnDefinition Width="*"/>
 | 
			
		||||
               <ColumnDefinition Width="64"/>
 | 
			
		||||
           </Grid.ColumnDefinitions>
 | 
			
		||||
            <TextBlock Grid.Column="0" Text="代理地址:" VerticalAlignment="Center"/>
 | 
			
		||||
            <TextBox Grid.Column="1"  VerticalAlignment="Center" Height="32" VerticalContentAlignment="Center"
 | 
			
		||||
                Text="{Binding ProxyAddress}"/>
 | 
			
		||||
            <TextBlock Grid.Column="2" Text="用户名:"  VerticalAlignment="Center" Margin="4,0,0,0"/>
 | 
			
		||||
            <TextBox Grid.Column="3"  VerticalAlignment="Center" Height="32" VerticalContentAlignment="Center"
 | 
			
		||||
                Text="{Binding ProxyUserName}"/>
 | 
			
		||||
            <TextBlock Grid.Column="4" Text="密码:"  VerticalAlignment="Center" Margin="4,0,0,0"/>
 | 
			
		||||
            <TextBox Grid.Column="5"  VerticalAlignment="Center" Height="32" VerticalContentAlignment="Center"
 | 
			
		||||
                Text="{Binding ProxyPassword}"/>
 | 
			
		||||
            <CheckBox x:Name="ProxyCb" Grid.Column="6" Content="代理" HorizontalAlignment="Right" VerticalAlignment="Center"
 | 
			
		||||
                IsChecked="{Binding IsProxyChecked}"/>
 | 
			
		||||
        </Grid>
 | 
			
		||||
 | 
			
		||||
        <Grid x:Name="OperateGrid" Grid.Row="1" Margin="0,10,0,0">
 | 
			
		||||
        <Grid x:Name="OperateGrid" Grid.Row="2" Margin="0,10,0,0">
 | 
			
		||||
            <Grid.ColumnDefinitions>
 | 
			
		||||
                <ColumnDefinition Width="*"/>
 | 
			
		||||
                <ColumnDefinition Width="200"/>
 | 
			
		||||
@@ -55,7 +79,7 @@
 | 
			
		||||
            </Grid>
 | 
			
		||||
        </Grid>
 | 
			
		||||
 | 
			
		||||
        <TextBox x:Name="InfoTb" Grid.Row="2" Margin="0,10,0,0" TextWrapping="Wrap"
 | 
			
		||||
        <TextBox x:Name="InfoTb" Grid.Row="3" Margin="0,10,0,0" TextWrapping="Wrap"
 | 
			
		||||
            HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"/>
 | 
			
		||||
    </Grid>
 | 
			
		||||
</Window>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net.Security;
 | 
			
		||||
using System.Security.Authentication;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using log4net;
 | 
			
		||||
@@ -14,7 +15,7 @@ namespace WebSocketTool.Client
 | 
			
		||||
        public event EventHandler<MessageEventArgs> MessageEvent;
 | 
			
		||||
        public event EventHandler<ErrorEventArgs> ErrorEvent;
 | 
			
		||||
        public event EventHandler<EventArgs> OpenEvent;
 | 
			
		||||
        public event EventHandler<CloseEventArgs> CloseEvent; 
 | 
			
		||||
        public event EventHandler<CloseEventArgs> CloseEvent;
 | 
			
		||||
 | 
			
		||||
        public SocketClient(string url)
 | 
			
		||||
        {
 | 
			
		||||
@@ -22,7 +23,7 @@ namespace WebSocketTool.Client
 | 
			
		||||
            mSocket = new WebSocket(url);
 | 
			
		||||
            if (url.StartsWith("wss"))
 | 
			
		||||
            {
 | 
			
		||||
                mSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls;
 | 
			
		||||
                mSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
 | 
			
		||||
            }
 | 
			
		||||
            mSocket.OnOpen += OnOpen;
 | 
			
		||||
            mSocket.OnClose += OnClose;
 | 
			
		||||
@@ -30,6 +31,35 @@ namespace WebSocketTool.Client
 | 
			
		||||
            mSocket.OnMessage += OnMessage;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #region Config
 | 
			
		||||
 | 
			
		||||
        public SocketClient SetServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("SetServerCertificateValidationCallback");
 | 
			
		||||
            mSocket.SslConfiguration.ServerCertificateValidationCallback += callback;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public SocketClient SetEnableSslProtocols(SslProtocols protocols)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"SetEnableSslProtocols:{protocols}");
 | 
			
		||||
            mSocket.SslConfiguration.EnabledSslProtocols = protocols;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public SocketClient SetHttpProxy(string address, string userName, string password)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"SetHttpProxy, address:{address}, userName:{userName}, password:{password}");
 | 
			
		||||
            if (address.StartsWith("http://"))
 | 
			
		||||
            {
 | 
			
		||||
                mSocket.SetProxy(address, userName, password);
 | 
			
		||||
            }
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        #region Event Handler
 | 
			
		||||
        private void OnMessage(object sender, MessageEventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"OnMessage, isPing:{e.IsPing}, isText:{e.IsText}, isBinary:{e.IsBinary}, data:{e.Data}, rawData:{e.RawData.Length}");
 | 
			
		||||
@@ -53,7 +83,40 @@ namespace WebSocketTool.Client
 | 
			
		||||
            Log.Info("OnOpen");
 | 
			
		||||
            OpenEvent?.Invoke(this, e);
 | 
			
		||||
        }
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        #region State
 | 
			
		||||
        public bool IsAlive()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"IsAlive:{mSocket?.IsAlive ?? false}");
 | 
			
		||||
            return mSocket?.IsAlive ?? false;
 | 
			
		||||
        }
 | 
			
		||||
        #endregion
 | 
			
		||||
        
 | 
			
		||||
        #region Operate
 | 
			
		||||
        public void Connect()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("Connect");
 | 
			
		||||
            mSocket.Connect();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void ConnectAsync()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("ConnectAsync");
 | 
			
		||||
            mSocket.ConnectAsync();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void Send(string content)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"send:{content}");
 | 
			
		||||
            if (content == null)
 | 
			
		||||
            {
 | 
			
		||||
                Log.Error("content is null!");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            mSocket.Send(content);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void Ping(string msg = null)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"ping:{msg}");
 | 
			
		||||
@@ -67,35 +130,6 @@ namespace WebSocketTool.Client
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Send(string content)
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"send:{content}");
 | 
			
		||||
            if (content == null)
 | 
			
		||||
            {
 | 
			
		||||
                Log.Error("content is null!");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            mSocket.Send(content);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool IsAlive()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info($"IsAlive:{mSocket?.IsAlive ?? false}");
 | 
			
		||||
            return mSocket?.IsAlive ?? false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Connect()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("Connect");
 | 
			
		||||
            mSocket.Connect();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void ConnectAsync()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("ConnectAsync");
 | 
			
		||||
            mSocket.ConnectAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Close()
 | 
			
		||||
        {
 | 
			
		||||
            Log.Info("Close");
 | 
			
		||||
@@ -107,5 +141,6 @@ namespace WebSocketTool.Client
 | 
			
		||||
            Log.Info("CloseAsync");
 | 
			
		||||
            mSocket.CloseAsync();
 | 
			
		||||
        }
 | 
			
		||||
        #endregion
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user