0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

.NetFrameworkのクライアントと.NetCoreのサーバでAzureSignalRを介して会話する

Last updated at Posted at 2023-06-17

やりたいこと

.NET Frameworkで動作するwindowsアプリと、サーバ(Azure上のWebApp)の間でリアルタイム通信をしたい。通信にはAzure SignalRを使いたい。

疑問

  • Azure SignalR ServiceはASP.NET Core SignalRをベースとしている。.NET Frameworkで動作するアプリからASP.NET Core SignalRを使用することは可能か?

確認結果

やってみる

サーバ

  • Azure上にSignalR Service/AppServiceをデプロイ
  • AppServiceにはこのサンプルをそのままデプロイ(Azure SignalR Serviceの接続文字列はデプロイしたものに合わせて変更する)

クライアント

  • これを参考に下記のようにコーディング
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private HubConnection _connection;
        public Form1()
        {
            InitializeComponent();
            _connection = new HubConnectionBuilder().WithUrl("https://webappのドメイン/Hubの名称").WithAutomaticReconnect().Build();
        }


        private async void btnConnect_Click(object sender, EventArgs e)
        {
            _connection.On<string, string>("broadcastMessage", (user, message) => {
                MessageBox.Show(message);
            });

            try
            {
                await _connection.StartAsync();
            }
            catch (Exception ex)
            {
            }
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            await _connection.InvokeAsync("broadcastMessage", txtName.Text, txtMessage.Text);
        }
    }
}

動かしてみる

通信できた!
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?