Compare commits

...

7 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
11 changed files with 144 additions and 63 deletions

View File

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

View File

@ -6,6 +6,7 @@ using log4net;
using WebSocketSharp;
using WebSocketTool.Base;
using WebSocketTool.Util;
using WebSocketTool.View.Dialog;
using LogManager = log4net.LogManager;
namespace WebSocketTool.Client
@ -21,6 +22,7 @@ namespace WebSocketTool.Client
this.view = view;
}
#region data bingding
private string mWsUrl;
public string WsUrl
{
@ -43,7 +45,7 @@ namespace WebSocketTool.Client
}
}
private long mPingTime = 1000;
private long mPingTime = 2000;
public long PingTime
{
get => mPingTime;
@ -102,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;
@ -183,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;
@ -195,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;
}
@ -212,32 +250,32 @@ 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()
{
App.RunOnUIThread(() => 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);
if (WsUrl.StartsWith("wss:"))
{
mClient.SetServerCertificateValidationCallback(CertificateValidationCallback);
mClient.AddServerCertificateValidationCallback(CertificateValidationCallback);
}
mClient.OpenEvent += ClientOnOpenEvent;
mClient.CloseEvent += ClientOnCloseEvent;
@ -250,9 +288,25 @@ 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($"ServerCertificateError:{errors}"); });
App.RunOnUIThread(() => { view.AppendInfo(FormatInfo($"ServerCertificateError:{errors}", 2)); });
Log.Info($"error:{errors}");
Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}");
Log.Info("ChainStatus:");
@ -272,17 +326,14 @@ namespace WebSocketTool.Client
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();
@ -313,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();
}
@ -323,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

@ -35,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

@ -51,5 +51,5 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2")]
[assembly: AssemblyFileVersion("1.0.2")]
[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>