Compare commits

...

10 Commits

Author SHA1 Message Date
fc08075e15 优化部分crash问题,更新为 1.0.4 2023-11-07 09:59:08 +08:00
702631b2d1 添加md 库文件,后续使用 2023-11-07 09:48:32 +08:00
1ee59b6417 修改版本为1.0.3 2023-08-02 11:48:23 +08:00
a4a80afe73 1.修改依赖库的Framework版本为4.5.2
2.修复部分逻辑的crash问题
3.提升版本号为1.0.3
2023-08-02 11:41:24 +08:00
aa3482f033 更新 'README.md' 2023-07-06 19:35:26 +08:00
c611622a63 更新 'README.md' 2023-07-06 19:26:31 +08:00
640b568e28 更新 'README.md' 2023-07-06 19:18:54 +08:00
87815d5bf6 修改 copyright信息为 2023 2023-04-21 18:27:21 +08:00
fdfd1872f4 修改版本号为 1.0.2 2023-04-21 18:07:20 +08:00
f07832d5f2 优化wss链接问题 2023-04-21 18:00:46 +08:00
11 changed files with 169 additions and 78 deletions

View File

@ -1,5 +1,18 @@
# WebSocketTool
#### 介绍
## 介绍
WebSocket测试工具
WebSocket测试工具, 后续逐步增加功能。
功能列表:
- [x] ws/wss链接发送消息
- [x] 定时 ping
## 版本
### v1.0.4
更新日志:
- 优化部分crash问题

View File

@ -1,10 +1,12 @@
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Timers;
using log4net;
using WebSocketSharp;
using WebSocketTool.Base;
using WebSocketTool.Util;
using WebSocketTool.View.Dialog;
using LogManager = log4net.LogManager;
namespace WebSocketTool.Client
@ -20,6 +22,7 @@ namespace WebSocketTool.Client
this.view = view;
}
#region data bingding
private string mWsUrl;
public string WsUrl
{
@ -42,7 +45,7 @@ namespace WebSocketTool.Client
}
}
private long mPingTime = 1000;
private long mPingTime = 2000;
public long PingTime
{
get => mPingTime;
@ -101,42 +104,6 @@ namespace WebSocketTool.Client
}
}
#endregion
public void Connect()
{
if (string.IsNullOrEmpty(WsUrl))
{
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n 请输入正确的WebSocket地址");
return;
}
if (mClient == null)
{
InitSocketClient();
}
if (mClient.IsAlive())
{
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n WebSocket已连接,请先断开上次连接");
return;
}
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Connect Socket");
if (IsProxyChecked)
{
view.AppendInfo($"use proxy: {ProxyAddress}");
mClient.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
}
mClient.ConnectAsync();
}
public void Send()
{
view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n {SendContent}");
mClient.Send(SendContent);
}
private bool mIsAlive = false;
private bool mIsConnectEnable = true;
@ -182,11 +149,83 @@ namespace WebSocketTool.Client
RaisePropertyChanged(nameof(IsPingEnable));
}
}
#endregion
#endregion
private string FormatInfo(string info, int type = 0)
{
var tag = "【Hint】";
switch (type)
{
case 1:
tag = "【Client】==>【Server】";
break;
case 2:
tag = "【Client】<==【Server】";
break;
}
return $"{tag} {TimeUtil.GetCurrentDateTime()} \n {info}";
}
public void Connect()
{
if (string.IsNullOrEmpty(WsUrl) || (!WsUrl.StartsWith("wss://") && !WsUrl.StartsWith("ws://")))
{
view.AppendInfo(FormatInfo("请输入正确的WebSocket地址"));
return;
}
if (mClient == null)
{
InitSocketClient();
}
if (IsProxyChecked)
{
if (!string.IsNullOrEmpty(ProxyAddress))
{
view.AppendInfo(FormatInfo($"设置代理地址:{ProxyAddress}"));
}
mClient?.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
}
if (mClient != null && mClient.IsAlive())
{
view.AppendInfo(FormatInfo("WebSocket已连接,请先断开"));
return;
}
if (IsProxyChecked)
{
if (!string.IsNullOrEmpty(ProxyAddress))
{
view.AppendInfo(FormatInfo($"使用代理服务器: {ProxyAddress}"));
mClient?.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
}
else
{
view.ShowToast("代理地址为空,请输入代理地址!");
view.AppendInfo(FormatInfo("代理地址为空,请输入正确的代理地址!"));
}
}
view.AppendInfo(FormatInfo($"开始连接Socket:{mWsUrl}"));
mClient.ConnectAsync();
}
public void Send()
{
view.AppendInfo(FormatInfo(SendContent, 1));
mClient.Send(SendContent);
}
public void Close()
{
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Close Socket");
mClient?.CloseAsync();
view.AppendInfo(FormatInfo("关闭Socket"));
if (mClient != null)
{
mClient.Close();
}
}
private Timer pingTimer;
@ -194,13 +233,13 @@ namespace WebSocketTool.Client
{
if (!mIsAlive)
{
view.AppendInfo("start ping failure: ws is not connected");
view.AppendInfo(FormatInfo("启动 ping 失败: ws 未链接成功!"));
return;
}
if (PingTime <= 0)
if (PingTime < 500)
{
view.AppendInfo("ping time interval must > 0");
view.AppendInfo(FormatInfo("ping间隔时间必须大于500ms"));
return;
}
@ -211,46 +250,33 @@ namespace WebSocketTool.Client
Ping();
};
pingTimer.Start();
view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StartPing, TimeSpan:{PingTime}");
view.AppendInfo(FormatInfo($"启动ping时间间隔{PingTime}"));
}
private void Ping(string msg = null)
{
mClient.Ping(msg);
App.RunOnUIThread(() => view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n Send Ping:{msg}"));
App.RunOnUIThread(() => view.AppendInfo(FormatInfo("发送ping", 1)));
}
public void StopPing()
{
view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StopPing");
App.RunOnUIThread(() => view.AppendInfo(FormatInfo("停止ping")));
if (pingTimer != null)
{
pingTimer.Stop();
pingTimer = null;
}
}
private void InitSocketClient()
{
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n InitSocketClient");
view.AppendInfo(FormatInfo("初始化Socket客户端"));
mClient = new SocketClient(WsUrl);
mClient.SetServerCertificateValidationCallback((sender, certificate, chain, errors) =>
if (WsUrl.StartsWith("wss:"))
{
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.AddServerCertificateValidationCallback(CertificateValidationCallback);
}
mClient.OpenEvent += ClientOnOpenEvent;
mClient.CloseEvent += ClientOnCloseEvent;
mClient.ErrorEvent += ClientOnErrorEvent;
@ -262,19 +288,52 @@ namespace WebSocketTool.Client
}
private void UninitSocketClient()
{
if (mClient != null)
{
if (WsUrl.StartsWith("wss:"))
{
mClient.RemoveServerCertificateValidationCallback(CertificateValidationCallback);
}
mClient.OpenEvent -= ClientOnOpenEvent;
mClient.CloseEvent -= ClientOnCloseEvent;
mClient.ErrorEvent -= ClientOnErrorEvent;
mClient.MessageEvent -= ClientOnMessageEvent;
mClient = null;
}
}
private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
App.RunOnUIThread(() => { view.AppendInfo(FormatInfo($"ServerCertificateError:{errors}", 2)); });
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;
}
private void ClientOnMessageEvent(object sender, MessageEventArgs e)
{
App.RunOnUIThread(() =>
{
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n {e.Data}");
});
App.RunOnUIThread(() => view.AppendInfo(FormatInfo(e.Data, 2)));
}
private void ClientOnErrorEvent(object sender, ErrorEventArgs e)
{
App.RunOnUIThread(() =>
{
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Error: {e.Message}");
view.AppendInfo(FormatInfo($"Socket Error: {e.Message}", 2));
SetState(false);
});
StopPing();
@ -305,8 +364,9 @@ namespace WebSocketTool.Client
{
App.RunOnUIThread(() =>
{
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Closed:{e.Code}:{e.Reason}");
view.AppendInfo(FormatInfo($"Socket Closed:{e.Code}:{e.Reason}", 2));
SetState(false);
UninitSocketClient();
});
StopPing();
}
@ -315,7 +375,7 @@ namespace WebSocketTool.Client
{
App.RunOnUIThread(() =>
{
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Connected");
view.AppendInfo(FormatInfo("Socket Connected", 2));
SetState(true);
});
}

View File

@ -75,11 +75,18 @@ namespace WebSocketTool.Client
{
viewModel.Close();
}
public Window GetWindow()
{
return this;
}
}
public interface IClientView
{
void ShowToast(string msg);
void AppendInfo(string info);
Window GetWindow();
}
}

View File

@ -16,10 +16,12 @@ namespace WebSocketTool.Client
public event EventHandler<ErrorEventArgs> ErrorEvent;
public event EventHandler<EventArgs> OpenEvent;
public event EventHandler<CloseEventArgs> CloseEvent;
public string Url { get; private set; }
public SocketClient(string url)
{
Log.Info($"create socket:{url}");
Url = url;
mSocket = new WebSocket(url);
if (url.StartsWith("wss"))
{
@ -33,13 +35,20 @@ namespace WebSocketTool.Client
#region Config
public SocketClient SetServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
public SocketClient AddServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
{
Log.Info("SetServerCertificateValidationCallback");
mSocket.SslConfiguration.ServerCertificateValidationCallback += callback;
return this;
}
public SocketClient RemoveServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
{
Log.Info("RemoveServerCertificateValidationCallback");
mSocket.SslConfiguration.ServerCertificateValidationCallback -= callback;
return this;
}
public SocketClient SetEnableSslProtocols(SslProtocols protocols)
{
Log.Info($"SetEnableSslProtocols:{protocols}");

View File

@ -27,6 +27,8 @@ namespace WebSocketTool
public MainWindow()
{
InitializeComponent();
ClientBtn_OnClick(null, null);
Close();
}
private void ClientBtn_OnClick(object sender, RoutedEventArgs e)

View File

@ -12,7 +12,7 @@ using System.Windows;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("DevWiki")]
[assembly: AssemblyProduct("WebSocketTool")]
[assembly: AssemblyCopyright("Copyright DevWiki © 2022")]
[assembly: AssemblyCopyright("Copyright DevWiki © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -51,5 +51,5 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyVersion("1.0.3")]
[assembly: AssemblyFileVersion("1.0.3")]

View File

@ -1,4 +1,4 @@
set version=1.0.0
set version=1.0.4
if not "%~1"=="" (
set version=%1

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,7 +9,7 @@
<OutputType>Library</OutputType>
<RootNamespace>WebSocketSharp</RootNamespace>
<AssemblyName>websocket-sharp</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>websocket-sharp.snk</AssemblyOriginatorKeyFile>
<FileUpgradeFlags>