The previous creation of the SIGNALR server is based on Web applications. Can you make the SIGNALR Server console application, WinForm, or Windows service?
The answer is yes.
Although it looked as if IIS and ASP. NET are the only environments that make up the managed SIGNALR service, it is not, in many cases, IIS and ASP.
Now, for example in the console program, the SIGNALR server is built into the console program.
How to implement a hub or persistent connection based SIGNALR service in a console program, first install the following package:
Install-package Microsoft.AspNet.SignalR.SelfHost
When the installation is complete, the project automatically loads the library files that need to be referenced.
Then in the program class of the console
1 usingSystem.Linq;2 usingSystem.Text;3 usingSystem.Threading.Tasks;4 5 namespaceConsoleApplication16 {7 class Program8 {9 Static voidMain (string[] args)Ten { One using(Webapp.start<startup> ("http://localhost:8888/") //Start the service, the access path is configured here. A { -Console.WriteLine ("Server running at http://localhost:8888/"); - console.readline (); the } - } - } -}
In the above code, we can see that we need a startup class Starup, which is the same as when we established a persistent connection or hub.
1 usingSystem;2 usingSystem.Threading.Tasks;3 usingMicrosoft.owin;4 usingOwin;5 6[Assembly:owinstartup (typeof(Startup))]7 8 Public classStartup9 {Ten Public voidConfiguration (Iappbuilder app) One { AApp. Mapsignalr<testconnection> ("/test");//Configure the specified persistent connection class, while mapping the access path is useful after - } -}
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Threading.Tasks;5 usingsystem.web;6 usingMicrosoft.AspNet.SignalR;7 8 Public classtestconnection:persistentconnection9 {Ten protected OverrideTask onconnected (irequest request,stringConnectionID) One { A returnConnection.send (ConnectionID,"welcome!"); - } - the protected OverrideTask onreceived (irequest request,stringConnectionID,stringdata) - { -Console.WriteLine (connectionid+"============="+data);//server accepts messages - returnconnection.broadcast (data);//Send a message to all clients + } -}
The above approach is to set up the SIGNALR service in the console and encode it in a persistent connection.
Of course, we can also use the hub to set up the server, the same way as before.
After the SIGNALR service is established, we need to build a client, before our clients are connected in HTML in the form of JS. Now, we will also build the client as a console application.
First build a console application and then install the following package:
Install-package microsoft.aspnet.signalr.client
Then write in the main method in the program class:
1 usingMicrosoft.AspNet.SignalR.Client.Transports;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingMicrosoft.AspNet.SignalR;8 usingMicrosoft.AspNet.SignalR.Client;9 namespaceConsoleApplication2Ten { One class Program A { - Static voidMain (string[] args) - { the Try - { - //Globalhost - varConnection =NewConnection ("http://localhost:8888/test")///server-configured access path and persistent connection mapping path + - connection. Start ();//Begin Connection +Connection. Received + =connection_received;//Configuring Accept Message Events A stringstr =""; at while(str = console.readline ())! ="") - { - connection. Send (str);//sends a message - } - - } in Catch(Exception ex) - { to + Console.WriteLine (ex. Message); - } the Console.read (); * } $ Panax Notoginseng Private Static voidConnection_received (stringobj) - { the Console.WriteLine (obj); + } A } the}
SIGNALR Getting Started multi-platform SIGNALR server