144 lines
4.9 KiB
C#
144 lines
4.9 KiB
C#
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");
|
|
}
|
|
}
|
|
}
|
|
}
|