using HotelPms.Data.Common.Dtos;
|
using Microsoft.AspNetCore.SignalR.Client;
|
using Microsoft.Extensions.DependencyInjection;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Text.Json;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
|
namespace HotelPms.SourceFactory
|
{
|
public partial class FormSignalR : Form
|
{
|
private HubConnection hubConnection;
|
|
public FormSignalR()
|
{
|
InitializeComponent();
|
}
|
|
private async void button1_Click(object sender, EventArgs e)
|
{
|
hubConnection = new HubConnectionBuilder()
|
//.WithUrl(NavigationManager.ToAbsoluteUri("/notifyhub"))
|
.WithUrl("https://localhost:44375/notifyhub")
|
.AddMessagePackProtocol()
|
.Build();
|
|
hubConnection.On<MessageDto>("ReceiveMessage", (data) =>
|
{
|
var encodedMsg = $"【{data.Id}】【{data.ConnectionId} ⇒ {data.DestConnectionId}】{data.Data};{data.Tag}";
|
listBox1.Items.Add(encodedMsg);
|
|
var options = new JsonSerializerOptions
|
{
|
WriteIndented = true
|
};
|
string json = JsonSerializer.Serialize(data, options);
|
System.Diagnostics.Debug.WriteLine(json);
|
});
|
|
await hubConnection.StartAsync();
|
label1.Text = hubConnection.ConnectionId;
|
}
|
|
private async Task Send()
|
{
|
MessageDto dto = new MessageDto()
|
{
|
Id = Guid.NewGuid(),
|
UserID = "Client1",
|
Data = textBox1.Text,
|
Tag = "1000",
|
};
|
await hubConnection.SendAsync("SendMessage", dto);
|
}
|
|
public bool IsConnected =>
|
hubConnection.State == HubConnectionState.Connected;
|
|
public async ValueTask DisposeAsync()
|
{
|
await hubConnection.DisposeAsync();
|
}
|
|
private void button3_Click(object sender, EventArgs e)
|
{
|
_= Send();
|
}
|
|
private void button2_Click(object sender, EventArgs e)
|
{
|
|
}
|
}
|
}
|