201903242305SignalR 2.4.0 example(.net framework 4.5) use persistent connection in WebForm and Windows Form

Complete Source Code:

Three projects are in this solution. There are SignalR Server、Windows Form、Web Form

All project use .net framework 4.5 and SignalR 2.4.0.

Develop Tools: Visual Studio 2017 

 

SignalR Server:

 Firstly, Create empty Web Form project. Add Startup.cs as below:

 

StartUp.cs use cors cross-domain, URL path use "/realtime/echo", 

ConnectionConfiguration EnableJSONP for crosss-domain

 

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;

[assembly: OwinStartup(typeof(SignalRServer.Startup))]
namespace SignalRServer
{
	public class Startup
	{
		public void Configuration(IAppBuilder app)
		{
			app.Map("/realtime/echo", map =>
			{
				map.UseCors(CorsOptions.AllowAll);
				var ConnectionConfiguration = new ConnectionConfiguration
				{					
					EnableJSONP = true
				};				
				map.RunSignalR(ConnectionConfiguration);
			});	
		}
	}
}

Windows Form:
persistentConnection = new Connection("http://localhost:20926/realtime/echo");
persistentConnection.Received += ReceivedMessage;
persistentConnection.Start().Wait();

 

Web Form:

use xdomain for cross-domain

<script src="Scripts/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-2.4.0.min.js" type="text/javascript"></script></p>
<title>SignalR</title>
<script type="text/javascript">

var bUseLocal = false;
var ConnectionID = "";

$(function () {

if (bUseLocal) {
connection = $.connection("http://localhost:20926/realtime/echo", '', false, { xdomain: true });


}

======

如果覺得這個程式對您有幫助的話,您可以用郵政劃撥到我的帳號06690594,贊助我的研究,謝謝!!

記得寫下你的名字給我。
======

 

回應
Blogger ADS
kiss radio
關鍵字





Powered by Xuite
    沒有新回應!