Compare commits
No commits in common. "fc08075e15f54b1de81cc128401834779bdbf61e" and "1ee59b641743be75b254ce3fc1f0e50cbde33ca4" have entirely different histories.
fc08075e15
...
1ee59b6417
12
README.md
12
README.md
@ -1,18 +1,10 @@
|
|||||||
# WebSocketTool
|
# WebSocketTool
|
||||||
|
|
||||||
## 介绍
|
#### 介绍
|
||||||
|
|
||||||
WebSocket测试工具, 后续逐步增加功能。
|
WebSocket测试工具, 后续逐步增加功能。
|
||||||
|
|
||||||
功能列表:
|
功能列表:
|
||||||
|
|
||||||
- [x] ws/wss链接发送消息
|
- [x] ws/wss链接发送消息
|
||||||
- [x] 定时 ping
|
- [x] 定时 ping
|
||||||
|
|
||||||
|
|
||||||
## 版本
|
|
||||||
|
|
||||||
### v1.0.4
|
|
||||||
|
|
||||||
更新日志:
|
|
||||||
- 优化部分crash问题
|
|
@ -22,7 +22,6 @@ namespace WebSocketTool.Client
|
|||||||
this.view = view;
|
this.view = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region data bingding
|
|
||||||
private string mWsUrl;
|
private string mWsUrl;
|
||||||
public string WsUrl
|
public string WsUrl
|
||||||
{
|
{
|
||||||
@ -45,7 +44,7 @@ namespace WebSocketTool.Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long mPingTime = 2000;
|
private long mPingTime = 1000;
|
||||||
public long PingTime
|
public long PingTime
|
||||||
{
|
{
|
||||||
get => mPingTime;
|
get => mPingTime;
|
||||||
@ -104,6 +103,50 @@ namespace WebSocketTool.Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public void Connect()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(WsUrl) || (!WsUrl.StartsWith("wss://") && !WsUrl.StartsWith("ws://")))
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(ProxyAddress))
|
||||||
|
{
|
||||||
|
view.AppendInfo($"Hint use proxy: {ProxyAddress}");
|
||||||
|
mClient.SetHttpProxy(ProxyAddress, ProxyUserName, ProxyPassword);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
view.ShowToast("请输入代理地址!");
|
||||||
|
view.AppendInfo($"use proxy address is empty!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mClient.ConnectAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send()
|
||||||
|
{
|
||||||
|
view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n {SendContent}");
|
||||||
|
mClient.Send(SendContent);
|
||||||
|
}
|
||||||
|
|
||||||
private bool mIsAlive = false;
|
private bool mIsAlive = false;
|
||||||
|
|
||||||
private bool mIsConnectEnable = true;
|
private bool mIsConnectEnable = true;
|
||||||
@ -149,83 +192,11 @@ namespace WebSocketTool.Client
|
|||||||
RaisePropertyChanged(nameof(IsPingEnable));
|
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()
|
public void Close()
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo("关闭Socket"));
|
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n Start Close Socket");
|
||||||
if (mClient != null)
|
mClient?.CloseAsync();
|
||||||
{
|
|
||||||
mClient.Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Timer pingTimer;
|
private Timer pingTimer;
|
||||||
@ -233,13 +204,13 @@ namespace WebSocketTool.Client
|
|||||||
{
|
{
|
||||||
if (!mIsAlive)
|
if (!mIsAlive)
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo("启动 ping 失败: ws 未链接成功!"));
|
view.AppendInfo("start ping failure: ws is not connected");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PingTime < 500)
|
if (PingTime <= 0)
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo("ping间隔时间必须大于500ms"));
|
view.AppendInfo("ping time interval must > 0");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,32 +221,32 @@ namespace WebSocketTool.Client
|
|||||||
Ping();
|
Ping();
|
||||||
};
|
};
|
||||||
pingTimer.Start();
|
pingTimer.Start();
|
||||||
view.AppendInfo(FormatInfo($"启动ping,时间间隔:{PingTime}"));
|
view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StartPing, TimeSpan:{PingTime}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Ping(string msg = null)
|
private void Ping(string msg = null)
|
||||||
{
|
{
|
||||||
mClient.Ping(msg);
|
mClient.Ping(msg);
|
||||||
App.RunOnUIThread(() => view.AppendInfo(FormatInfo("发送ping", 1)));
|
App.RunOnUIThread(() => view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n Send Ping:{msg}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopPing()
|
public void StopPing()
|
||||||
{
|
{
|
||||||
App.RunOnUIThread(() => view.AppendInfo(FormatInfo("停止ping")));
|
App.RunOnUIThread(() => view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StopPing"));
|
||||||
if (pingTimer != null)
|
if (pingTimer != null)
|
||||||
{
|
{
|
||||||
pingTimer.Stop();
|
pingTimer.Stop();
|
||||||
pingTimer = null;
|
pingTimer = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitSocketClient()
|
private void InitSocketClient()
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo("初始化Socket客户端"));
|
view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n InitSocketClient");
|
||||||
mClient = new SocketClient(WsUrl);
|
mClient = new SocketClient(WsUrl);
|
||||||
if (WsUrl.StartsWith("wss:"))
|
if (WsUrl.StartsWith("wss:"))
|
||||||
{
|
{
|
||||||
mClient.AddServerCertificateValidationCallback(CertificateValidationCallback);
|
mClient.SetServerCertificateValidationCallback(CertificateValidationCallback);
|
||||||
}
|
}
|
||||||
mClient.OpenEvent += ClientOnOpenEvent;
|
mClient.OpenEvent += ClientOnOpenEvent;
|
||||||
mClient.CloseEvent += ClientOnCloseEvent;
|
mClient.CloseEvent += ClientOnCloseEvent;
|
||||||
@ -288,25 +259,9 @@ 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)
|
private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||||||
{
|
{
|
||||||
App.RunOnUIThread(() => { view.AppendInfo(FormatInfo($"ServerCertificateError:{errors}", 2)); });
|
App.RunOnUIThread(() => { view.AppendInfo($"ServerCertificateError:{errors}"); });
|
||||||
Log.Info($"error:{errors}");
|
Log.Info($"error:{errors}");
|
||||||
Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}");
|
Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}");
|
||||||
Log.Info("ChainStatus:");
|
Log.Info("ChainStatus:");
|
||||||
@ -326,14 +281,17 @@ namespace WebSocketTool.Client
|
|||||||
|
|
||||||
private void ClientOnMessageEvent(object sender, MessageEventArgs e)
|
private void ClientOnMessageEvent(object sender, MessageEventArgs e)
|
||||||
{
|
{
|
||||||
App.RunOnUIThread(() => view.AppendInfo(FormatInfo(e.Data, 2)));
|
App.RunOnUIThread(() =>
|
||||||
|
{
|
||||||
|
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n {e.Data}");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClientOnErrorEvent(object sender, ErrorEventArgs e)
|
private void ClientOnErrorEvent(object sender, ErrorEventArgs e)
|
||||||
{
|
{
|
||||||
App.RunOnUIThread(() =>
|
App.RunOnUIThread(() =>
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo($"Socket Error: {e.Message}", 2));
|
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Error: {e.Message}");
|
||||||
SetState(false);
|
SetState(false);
|
||||||
});
|
});
|
||||||
StopPing();
|
StopPing();
|
||||||
@ -364,9 +322,8 @@ namespace WebSocketTool.Client
|
|||||||
{
|
{
|
||||||
App.RunOnUIThread(() =>
|
App.RunOnUIThread(() =>
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo($"Socket Closed:{e.Code}:{e.Reason}", 2));
|
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Closed:{e.Code}:{e.Reason}");
|
||||||
SetState(false);
|
SetState(false);
|
||||||
UninitSocketClient();
|
|
||||||
});
|
});
|
||||||
StopPing();
|
StopPing();
|
||||||
}
|
}
|
||||||
@ -375,7 +332,7 @@ namespace WebSocketTool.Client
|
|||||||
{
|
{
|
||||||
App.RunOnUIThread(() =>
|
App.RunOnUIThread(() =>
|
||||||
{
|
{
|
||||||
view.AppendInfo(FormatInfo("Socket Connected", 2));
|
view.AppendInfo($"Server {TimeUtil.GetCurrentDateTime()} \n Socket Connected");
|
||||||
SetState(true);
|
SetState(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -35,20 +35,13 @@ namespace WebSocketTool.Client
|
|||||||
|
|
||||||
#region Config
|
#region Config
|
||||||
|
|
||||||
public SocketClient AddServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
|
public SocketClient SetServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
|
||||||
{
|
{
|
||||||
Log.Info("SetServerCertificateValidationCallback");
|
Log.Info("SetServerCertificateValidationCallback");
|
||||||
mSocket.SslConfiguration.ServerCertificateValidationCallback += callback;
|
mSocket.SslConfiguration.ServerCertificateValidationCallback += callback;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketClient RemoveServerCertificateValidationCallback(RemoteCertificateValidationCallback callback)
|
|
||||||
{
|
|
||||||
Log.Info("RemoveServerCertificateValidationCallback");
|
|
||||||
mSocket.SslConfiguration.ServerCertificateValidationCallback -= callback;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SocketClient SetEnableSslProtocols(SslProtocols protocols)
|
public SocketClient SetEnableSslProtocols(SslProtocols protocols)
|
||||||
{
|
{
|
||||||
Log.Info($"SetEnableSslProtocols:{protocols}");
|
Log.Info($"SetEnableSslProtocols:{protocols}");
|
||||||
|
@ -27,8 +27,6 @@ namespace WebSocketTool
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
ClientBtn_OnClick(null, null);
|
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClientBtn_OnClick(object sender, RoutedEventArgs e)
|
private void ClientBtn_OnClick(object sender, RoutedEventArgs e)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
set version=1.0.4
|
set version=1.0.3
|
||||||
|
|
||||||
if not "%~1"=="" (
|
if not "%~1"=="" (
|
||||||
set version=%1
|
set version=%1
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user