SignalR控制台应用程序示例
有一个控制台或winform应用程序使用signalR向.net中心发送消息的一个小例子? 我已经尝试过.net的例子,并且看过了wiki,但是对于hub(.net)和客户端(控制台应用程序)之间的关系没有任何意义(找不到这个例子)。 该应用程序只需要集线器的地址和名称来连接?
如果有人能够提供一小段代码来显示应用程序连接到集线器并发送“Hello World”或.net集线器收到的东西?
PS。 我有一个标准的集线器聊天的例子,运行良好,如果我尝试在Cs中分配一个集线器名称,它停止工作,即[HubName(“testing”)],你知道这个的原因?
谢谢。
当前控制台应用代码。
static void Main(string[] args) { //Set connection var connection = new HubConnection("http://localhost:41627/"); //Make proxy to hub based on hub name on server var myHub = connection.CreateProxy("chat"); //Start connection connection.Start().ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException()); } else { Console.WriteLine("Connected"); } }).Wait(); //connection.StateChanged += connection_StateChanged; myHub.Invoke("Send", "HELLO World ").ContinueWith(task => { if(task.IsFaulted) { Console.WriteLine("There was an error calling send: {0}",task.Exception.GetBaseException()); } else { Console.WriteLine("Send Complete."); } }); }
Hub服务器。 (不同的项目工作区)
public class Chat : Hub { public void Send(string message) { // Call the addMessage method on all clients Clients.addMessage(message); } }
Info维基为此是http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client
首先你应该在你的客户端应用程序的服务器应用程序和SignalR.Client上安装SignalR.Host.Self:
PM> Install-Package SignalR.Hosting.Self -Version 0.5.2
PM> Install-Package Microsoft.AspNet.SignalR.Client
然后将下面的代码添加到您的项目;)
(以pipe理员身份运行项目)
服务器控制台应用
using System; using SignalR.Hubs; namespace SignalR.Hosting.Self.Samples { class Program { static void Main(string[] args) { string url = "http://127.0.0.1:8088/"; var server = new Server(url); // Map the default hub url (/signalr) server.MapHubs(); // Start the server server.Start(); Console.WriteLine("Server running on {0}", url); // Keep going until somebody hits 'x' while (true) { ConsoleKeyInfo ki = Console.ReadKey(true); if (ki.Key == ConsoleKey.X) { break; } } } [HubName("CustomHub")] public class MyHub : Hub { public string Send(string message) { return message; } public void DoSomething(string param) { Clients.addMessage(param); } } } }
客户端控制台应用
using System; using SignalR.Client.Hubs; namespace SignalRConsoleApp { internal class Program { private static void Main(string[] args) { //Set connection var connection = new HubConnection("http://127.0.0.1:8088/"); //Make proxy to hub based on hub name on server var myHub = connection.CreateHubProxy("CustomHub"); //Start connection connection.Start().ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException()); } else { Console.WriteLine("Connected"); } }).Wait(); myHub.Invoke<string>("Send", "HELLO World ").ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine("There was an error calling send: {0}", task.Exception.GetBaseException()); } else { Console.WriteLine(task.Result); } }); myHub.On<string>("addMessage", param => { Console.WriteLine(param); }); myHub.Invoke<string>("DoSomething", "I'm doing something!!!").Wait(); Console.Read(); connection.Stop(); } } }
自我主人现在使用欧文。 结帐http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-self-host设置服务器。; 它与上面的客户端代码兼容。
SignalR 2.2.1(2017年5月)
服务器
安装程序包Microsoft.AspNet.SignalR.SelfHost – 版本2.2.1
[assembly: OwinStartup(typeof(Program.Startup))] namespace ConsoleApplication116_SignalRServer { class Program { static IDisposable SignalR; static void Main(string[] args) { string url = "http://127.0.0.1:8088"; SignalR = WebApp.Start(url); Console.ReadKey(); } public class Startup { public void Configuration(IAppBuilder app) { app.UseCors(CorsOptions.AllowAll); app.MapSignalR(); } } [HubName("MyHub")] public class MyHub : Hub { public void Send(string name, string message) { Clients.All.addMessage(name, message); } } } }
客户
(几乎与Mehrdad Bahrainy的答复相同)
安装程序包Microsoft.AspNet.SignalR.Client – 版本2.2.1
namespace ConsoleApplication116_SignalRClient { class Program { private static void Main(string[] args) { var connection = new HubConnection("http://127.0.0.1:8088/"); var myHub = connection.CreateHubProxy("MyHub"); Console.WriteLine("Enter your name"); string name = Console.ReadLine(); connection.Start().ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException()); } else { Console.WriteLine("Connected"); myHub.On<string, string>("addMessage", (s1, s2) => { Console.WriteLine(s1 + ": " + s2); }); while (true) { string message = Console.ReadLine(); if (string.IsNullOrEmpty(message)) { break; } myHub.Invoke<string>("Send", name, message).ContinueWith(task1 => { if (task1.IsFaulted) { Console.WriteLine("There was an error calling send: {0}", task1.Exception.GetBaseException()); } else { Console.WriteLine(task1.Result); } }); } } }).Wait(); Console.Read(); connection.Stop(); } } }