. Net SignalR winform push broadcast, signalrwinform
Recently, I am working on a project and need to use the server to actively push the data to the client. I first used my own Remoting, but later I found that I had mounted the server to the Internet, so I was monkey's brother, later, I tried WCF. Although I could push it, I pushed it several times, and I found SignalR. I checked the information about the communication framework myself, which is very convenient, however, there is very little information about winform, which is all web-based, so as not to repeat the same mistakes, so I will write this article.
Server code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using Microsoft. AspNet. SignalR;
Namespace SignalRServer
{
Public class BroadcastContext
{
// In Singleton mode, all clients share this instance for broadcast.
Private static readonly BroadcastContext instance =
New BroadcastContext (GlobalHost. ConnectionManager. GetHubContext <BroadcastHub> ());
Private readonly IHubContext context;
// Function business type
Private readonly FunctionApp functionApp;
Public static BroadcastContext Instance
{
Get {return instance ;}
}
Private BroadcastContext (IHubContext context)
{
This. context = context;
FunctionApp = new FunctionApp (context );
FunctionApp. Action ();
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using Microsoft. AspNet. SignalR;
Namespace SignalRServer
{
Public class BroadcastHub: Hub
{
Private readonly BroadcastContext broadcastContext;
Public BroadcastHub ()
{
BroadcastContext = BroadcastContext. Instance;
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using Microsoft. Owin. Cors;
Using Owin;
Namespace SignalRServer
{
Public class Startup
{
Public void Configuration (IAppBuilder app)
{
App. UseCors (CorsOptions. AllowAll );
App. MapSignalR ();
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using Microsoft. AspNet. SignalR;
Using System. Threading;
Namespace SignalRServer
{
Public class FunctionApp
{
// Context
Private readonly IHubContext context;
Public FunctionApp (IHubContext context)
{
This. context = context;
}
String [] actons = {"get up at", "broadcast gymnastics at", "Breakfast at", "What should I do "};
Random r = new Random ();
Public void Action ()
{
// Test the Broadcast Function
Task. Run () => Start ());
}
Private void Start ()
{
While (true)
{
Thread. Sleep (500 );
// The method name called by the client must be consistent
Context. Clients. All. ReceiveMsg ("ClientId =>" + r. Next (1, 20), "Message =>" + actons [r. Next (0, 4)]);
}
}
}
}
Using Microsoft. Owin. Hosting;
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 SignalRServer
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
String url = "http: // 192.168.1.77: 8086 ";
Private void Form1_Load (object sender, EventArgs e)
{
WebApp. Start <Startup> (url );
LblInfo. Text = "service started successfully ";
}
}
}
Client code:
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;
Using Microsoft. AspNet. SignalR. Client;
Namespace SignalRClient
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
HubConnection connection = null;
IHubProxy myHub = null;
String url = "http: // 192.168.1.77: 8086 ";
Private void Form1_Load (object sender, EventArgs e)
{
Connection = new HubConnection (url );
// The class name must be consistent with the server name
MyHub = connection. CreateHubProxy ("BroadcastHub ");
// The method name must be the same as that on the server.
MyHub. On <string, string> ("ReceiveMsg", ReceiveMsg );
Connection. Start (). ContinueWith (task =>
{
If (! Task. IsFaulted)
{
RichTextBox1.Text = "connection to server successful! \ R \ n ";
}
Else
{
RichTextBox1.Text = "An error occurred while connecting to the server. Check whether the server is enabled! \ R \ n ";
}
}). Wait ();
}
Private void ReceiveMsg (string clientId, string message)
{
RichTextBox1.Text + = clientId + "" + message + "\ r \ n ";
}
}
}
Download complete code