优化部分错误

This commit is contained in:
DevWiki 2022-04-05 19:37:12 +08:00
parent fcb553bfe4
commit f77a512451
5 changed files with 37 additions and 30 deletions

View File

@ -1,15 +1,17 @@
using System; using System;
using System.Windows.Threading; using System.Timers;
using log4net; using log4net;
using WebSocketSharp; using WebSocketSharp;
using WebSocketTool.Base; using WebSocketTool.Base;
using WebSocketTool.Util;
using LogManager = log4net.LogManager;
namespace WebSocketTool.Client namespace WebSocketTool.Client
{ {
public class ClientViewModel : BaseViewModel public class ClientViewModel : BaseViewModel
{ {
private static readonly ILog Log = LogManager.GetLogger(nameof(ClientViewModel)); private static readonly ILog Log = LogManager.GetLogger(nameof(ClientViewModel));
private IClientView view; private readonly IClientView view;
private SocketClient mClient; private SocketClient mClient;
public ClientViewModel(IClientView view) public ClientViewModel(IClientView view)
@ -54,7 +56,7 @@ namespace WebSocketTool.Client
{ {
if (string.IsNullOrEmpty(WsUrl)) if (string.IsNullOrEmpty(WsUrl))
{ {
view.AppendInfo("请输入正确的WebSocket地址"); view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n 请输入正确的WebSocket地址");
return; return;
} }
@ -65,17 +67,17 @@ namespace WebSocketTool.Client
if (mClient.IsAlive()) if (mClient.IsAlive())
{ {
view.AppendInfo("WebSocket已连接,请先断开上次连接"); view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n WebSocket已连接,请先断开上次连接");
return; return;
} }
view.AppendInfo($"<=== start connect"); view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Connect Socket");
mClient.ConnectAsync(); mClient.ConnectAsync();
} }
public void Send() public void Send()
{ {
view.AppendInfo($"<=== socket send:{SendContent}"); view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n {SendContent}");
mClient.Send(SendContent); mClient.Send(SendContent);
} }
@ -127,11 +129,11 @@ namespace WebSocketTool.Client
public void Close() public void Close()
{ {
view.AppendInfo($"<=== start close"); view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Close Socket");
mClient?.CloseAsync(); mClient?.CloseAsync();
} }
private DispatcherTimer pingTimer; private Timer pingTimer;
public void StartPing() public void StartPing()
{ {
if (!mIsAlive) if (!mIsAlive)
@ -139,30 +141,26 @@ namespace WebSocketTool.Client
view.AppendInfo("start ping failure: ws is not connected"); view.AppendInfo("start ping failure: ws is not connected");
return; return;
} }
if (pingTimer?.IsEnabled ?? false)
{ pingTimer = new Timer {Interval = PingTime, AutoReset = false};
pingTimer.Stop(); pingTimer.Elapsed += (s, e) =>
}
pingTimer = new DispatcherTimer();
pingTimer.Interval = TimeSpan.FromMilliseconds(PingTime);
pingTimer.Tick += (sender, args) =>
{ {
Ping(); Ping();
}; };
pingTimer.Start(); pingTimer.Start();
view.AppendInfo($"<===StartPing, time:{PingTime}"); view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StartPing, TimeSpan:{PingTime}");
} }
private void Ping(string msg = null) private void Ping(string msg = null)
{ {
view.AppendInfo($"<=== ping:{msg}");
mClient.Ping(msg); mClient.Ping(msg);
App.RunOnUIThread(() => view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n Send Ping:{msg}"));
} }
public void StopPing() public void StopPing()
{ {
view.AppendInfo("<===StopPing"); view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StopPing");
if (pingTimer?.IsEnabled ?? false) if (pingTimer != null)
{ {
pingTimer.Stop(); pingTimer.Stop();
pingTimer = null; pingTimer = null;
@ -171,7 +169,7 @@ namespace WebSocketTool.Client
private void InitSocketClient() private void InitSocketClient()
{ {
view.AppendInfo("InitSocketClient"); view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n InitSocketClient");
mClient = new SocketClient(WsUrl); mClient = new SocketClient(WsUrl);
mClient.OpenEvent += ClientOnOpenEvent; mClient.OpenEvent += ClientOnOpenEvent;
mClient.CloseEvent += ClientOnCloseEvent; mClient.CloseEvent += ClientOnCloseEvent;
@ -183,7 +181,7 @@ namespace WebSocketTool.Client
{ {
App.RunOnUIThread(() => App.RunOnUIThread(() =>
{ {
view.AppendInfo($"===> receive message: {e.Data}"); view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n {e.Data}");
}); });
} }
@ -191,7 +189,7 @@ namespace WebSocketTool.Client
{ {
App.RunOnUIThread(() => App.RunOnUIThread(() =>
{ {
view.AppendInfo($"===> socket error: {e.Message}"); view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Error: {e.Message}");
SetState(false); SetState(false);
}); });
StopPing(); StopPing();
@ -222,7 +220,7 @@ namespace WebSocketTool.Client
{ {
App.RunOnUIThread(() => App.RunOnUIThread(() =>
{ {
view.AppendInfo($"===> socket closed:{e.Code}:{e.Reason}"); view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Closed:{e.Code}:{e.Reason}");
SetState(false); SetState(false);
}); });
StopPing(); StopPing();
@ -232,7 +230,7 @@ namespace WebSocketTool.Client
{ {
App.RunOnUIThread(() => App.RunOnUIThread(() =>
{ {
view.AppendInfo($"<=== socket connected"); view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Connected");
SetState(true); SetState(true);
}); });
} }

View File

@ -6,6 +6,7 @@
xmlns:local="clr-namespace:WebSocketTool.Client" xmlns:local="clr-namespace:WebSocketTool.Client"
mc:Ignorable="d" WindowStartupLocation="CenterScreen" mc:Ignorable="d" WindowStartupLocation="CenterScreen"
Closed="ClientWindow_OnClosed" Closed="ClientWindow_OnClosed"
d:DataContext="{d:DesignInstance d:Type=local:ClientViewModel}"
Title="ClientWindow" Height="450" Width="800"> Title="ClientWindow" Height="450" Width="800">
<Grid Margin="5"> <Grid Margin="5">
<Grid.RowDefinitions> <Grid.RowDefinitions>
@ -40,7 +41,7 @@
<RowDefinition Height="36"/> <RowDefinition Height="36"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TextBlock Text="&lt;---------------------" VerticalAlignment="Center"/> <TextBlock Text="&lt;---------------------" VerticalAlignment="Center"/>
<Button x:Name="SendContentBtn" Width="80" Height="32" Content="发送" Click="SendContentBtn_OnClick" <Button Grid.Row="0" x:Name="SendContentBtn" Width="80" Height="32" Content="发送" Click="SendContentBtn_OnClick"
HorizontalAlignment="Right" Cursor="Hand" IsEnabled="{Binding IsCloseEnable, Mode=OneWay}"/> HorizontalAlignment="Right" Cursor="Hand" IsEnabled="{Binding IsCloseEnable, Mode=OneWay}"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Grid.Row="1" HorizontalAlignment="Left"> <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Grid.Row="1" HorizontalAlignment="Left">
<TextBlock Text="时间间隔:"></TextBlock> <TextBlock Text="时间间隔:"></TextBlock>

View File

@ -22,7 +22,7 @@ namespace WebSocketTool.Client
public partial class ClientWindow : Window, IClientView public partial class ClientWindow : Window, IClientView
{ {
private static readonly ILog Log = LogManager.GetLogger(nameof(ClientWindow)); private static readonly ILog Log = LogManager.GetLogger(nameof(ClientWindow));
private ClientViewModel viewModel; private readonly ClientViewModel viewModel;
public ClientWindow() public ClientWindow()
{ {
InitializeComponent(); InitializeComponent();

View File

@ -10,7 +10,7 @@ namespace WebSocketTool.Client
public class SocketClient public class SocketClient
{ {
private static readonly ILog Log = LogManager.GetLogger(nameof(SocketClient)); private static readonly ILog Log = LogManager.GetLogger(nameof(SocketClient));
private WebSocket mSocket; private readonly WebSocket mSocket;
public event EventHandler<MessageEventArgs> MessageEvent; public event EventHandler<MessageEventArgs> MessageEvent;
public event EventHandler<ErrorEventArgs> ErrorEvent; public event EventHandler<ErrorEventArgs> ErrorEvent;
public event EventHandler<EventArgs> OpenEvent; public event EventHandler<EventArgs> OpenEvent;
@ -20,7 +20,10 @@ namespace WebSocketTool.Client
{ {
Log.Info($"create socket:{url}"); Log.Info($"create socket:{url}");
mSocket = new WebSocket(url); mSocket = new WebSocket(url);
if (url.StartsWith("wss"))
{
mSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; mSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
}
mSocket.OnOpen += OnOpen; mSocket.OnOpen += OnOpen;
mSocket.OnClose += OnClose; mSocket.OnClose += OnClose;
mSocket.OnError += OnError; mSocket.OnError += OnError;

View File

@ -5,9 +5,14 @@ namespace WebSocketTool.Util
{ {
public class TimeUtil public class TimeUtil
{ {
public static string GetLogTime() public static string GetCurrentTime()
{ {
return DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.ms]"); return DateTime.Now.ToString("HH:mm:ss.ms");
}
public static string GetCurrentDateTime()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ms");
} }
} }
} }