diff --git a/TEst Stefan/SignalRForm/Form1.Designer.cs b/TEst Stefan/SignalRForm/Form1.Designer.cs new file mode 100644 index 0000000..6ce495e --- /dev/null +++ b/TEst Stefan/SignalRForm/Form1.Designer.cs @@ -0,0 +1,129 @@ +namespace SignalRTest2 +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + connectButton = new Button(); + messagesList = new ListBox(); + sendButton = new Button(); + userTextBox = new TextBox(); + messageTextBox = new TextBox(); + label1 = new Label(); + label2 = new Label(); + SuspendLayout(); + // + // connectButton + // + connectButton.Location = new Point(470, 12); + connectButton.Name = "connectButton"; + connectButton.Size = new Size(75, 23); + connectButton.TabIndex = 0; + connectButton.Text = "Connect"; + connectButton.UseVisualStyleBackColor = true; + connectButton.Click += connectButton_Click; + // + // messagesList + // + messagesList.FormattingEnabled = true; + messagesList.ItemHeight = 15; + messagesList.Location = new Point(12, 70); + messagesList.Name = "messagesList"; + messagesList.Size = new Size(530, 304); + messagesList.TabIndex = 1; + // + // sendButton + // + sendButton.Location = new Point(470, 41); + sendButton.Name = "sendButton"; + sendButton.Size = new Size(75, 23); + sendButton.TabIndex = 2; + sendButton.Text = "Senden"; + sendButton.UseVisualStyleBackColor = true; + sendButton.Click += sendButton_Click; + // + // userTextBox + // + userTextBox.Location = new Point(74, 12); + userTextBox.Name = "userTextBox"; + userTextBox.Size = new Size(100, 23); + userTextBox.TabIndex = 3; + // + // messageTextBox + // + messageTextBox.Location = new Point(74, 41); + messageTextBox.Name = "messageTextBox"; + messageTextBox.Size = new Size(390, 23); + messageTextBox.TabIndex = 4; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(12, 16); + label1.Name = "label1"; + label1.Size = new Size(53, 15); + label1.TabIndex = 5; + label1.Text = "Benutzer"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(12, 44); + label2.Name = "label2"; + label2.Size = new Size(59, 15); + label2.TabIndex = 6; + label2.Text = "Nachricht"; + // + // Form1 + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(554, 396); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(messageTextBox); + Controls.Add(userTextBox); + Controls.Add(sendButton); + Controls.Add(messagesList); + Controls.Add(connectButton); + Name = "Form1"; + Text = "Form1"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button connectButton; + private ListBox messagesList; + private Button sendButton; + private TextBox userTextBox; + private TextBox messageTextBox; + private Label label1; + private Label label2; + } +} diff --git a/TEst Stefan/SignalRForm/Form1.cs b/TEst Stefan/SignalRForm/Form1.cs new file mode 100644 index 0000000..0d154a0 --- /dev/null +++ b/TEst Stefan/SignalRForm/Form1.cs @@ -0,0 +1,68 @@ +using System; +using System.Threading.Tasks; +using System.Windows; +using Microsoft.AspNetCore.SignalR.Client; + +/// Gefunden in: +/// https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-9.0&tabs=visual-studio +/// Da ist auch beschrieben, wie ein automatischer Reconnect gemacht wird + +namespace SignalRTest2 +{ + public partial class Form1 : Form + { + HubConnection connection; + public Form1() + { + InitializeComponent(); + + connection = new HubConnectionBuilder() + .WithUrl("https://localhost:7297/chathub") + // .WithAutomaticReconnect() + .Build(); + + connection.Closed += async (error) => + { + await Task.Delay(new Random().Next(0, 5) * 1000); + await connection.StartAsync(); + }; + } + + private async void connectButton_Click(object sender, EventArgs e) + { + connection.On("ReceiveMessage", (user, message) => + { + this.Invoke(() => + { + var newMessage = $"{user}: {message}"; + messagesList.Items.Add(newMessage); + }); + }); + + try + { + await connection.StartAsync(); + messagesList.Items.Add("Connection started"); + connectButton.Enabled = false; + sendButton.Enabled = true; + } + catch (Exception ex) + { + messagesList.Items.Add(ex.Message); + } + } + + private async void sendButton_Click(object sender, EventArgs e) + { + try + { + await connection.InvokeAsync("SendMessage", + userTextBox.Text, messageTextBox.Text); + } + catch (Exception ex) + { + messagesList.Items.Add(ex.Message); + } + } + } +} diff --git a/TEst Stefan/SignalRForm/Form1.resx b/TEst Stefan/SignalRForm/Form1.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/TEst Stefan/SignalRForm/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TEst Stefan/SignalRForm/Program.cs b/TEst Stefan/SignalRForm/Program.cs new file mode 100644 index 0000000..c460e32 --- /dev/null +++ b/TEst Stefan/SignalRForm/Program.cs @@ -0,0 +1,17 @@ +namespace SignalRTest2 +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/TEst Stefan/SignalRForm/SignalRTest2.csproj b/TEst Stefan/SignalRForm/SignalRTest2.csproj new file mode 100644 index 0000000..caa1bb2 --- /dev/null +++ b/TEst Stefan/SignalRForm/SignalRTest2.csproj @@ -0,0 +1,15 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + + + + + \ No newline at end of file diff --git a/TEst Stefan/SignalRForm/SignalRTest2.sln b/TEst Stefan/SignalRForm/SignalRTest2.sln new file mode 100644 index 0000000..3cf95e3 --- /dev/null +++ b/TEst Stefan/SignalRForm/SignalRTest2.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36408.4 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignalRTest2", "SignalRTest2.csproj", "{B4A2ED86-E104-4BEB-B2DA-074F3AEE4E2A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B4A2ED86-E104-4BEB-B2DA-074F3AEE4E2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4A2ED86-E104-4BEB-B2DA-074F3AEE4E2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4A2ED86-E104-4BEB-B2DA-074F3AEE4E2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4A2ED86-E104-4BEB-B2DA-074F3AEE4E2A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {51002EE5-C283-4208-BA56-2F2174A9FFA9} + EndGlobalSection +EndGlobal