add(tools):添加signalR调试工具
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using System.Text.Json;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SignalRTest
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
private HubConnection _connection;
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private async void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection = new HubConnectionBuilder()
|
||||
.WithUrl(txtUrl.Text, options =>
|
||||
{
|
||||
options.AccessTokenProvider = () =>
|
||||
Task.FromResult(TokenTxt.Text);
|
||||
})
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
|
||||
_connection.On<string, string>("ReceiveMessage", (user, message) =>
|
||||
{
|
||||
Historylab.Invoke(new Action(() =>
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Green;
|
||||
Historylab.AppendText("接收消息:" + message + "\n");
|
||||
}));
|
||||
});
|
||||
|
||||
_connection.Reconnecting += (ex) =>
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("重连中..." + "\n");
|
||||
return System.Threading.Tasks.Task.CompletedTask;
|
||||
};
|
||||
|
||||
_connection.Reconnected += (id) =>
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("重连成功!" + "\n");
|
||||
return System.Threading.Tasks.Task.CompletedTask;
|
||||
};
|
||||
|
||||
await _connection.StartAsync();
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("连接成功!" + "\n");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("连接失败:" + ex.Message + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
private async void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
await _connection.StopAsync();
|
||||
await _connection.DisposeAsync();
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("断开连接!" + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
private async void SendMsg_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_connection == null || _connection.State != HubConnectionState.Connected)
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText("请先连接!" + "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var method = MethodTxt.Text.Trim();
|
||||
var paramText = ParamsTxt.Text.Trim();
|
||||
|
||||
object[] parameters = string.IsNullOrWhiteSpace(paramText)
|
||||
? Array.Empty<object>()
|
||||
: JsonSerializer.Deserialize<object[]>(paramText);
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Red;
|
||||
Historylab.AppendText($"Invoke: {method} {paramText}" + "\n");
|
||||
await _connection.InvokeCoreAsync(method,parameters);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 移动光标到文本末尾
|
||||
Historylab.SelectionStart = Historylab.TextLength;
|
||||
Historylab.SelectionLength = 0;
|
||||
|
||||
Historylab.SelectionColor = Color.Blue;
|
||||
Historylab.AppendText($"发送失败: {ex.Message}" + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user