asp.net-core – 使用新的signalR for DotNet核心从控制台App向
发布时间:2020-12-16 06:34:31 所属栏目:asp.Net 来源:网络整理
导读:我正在使用 https://blogs.msdn.microsoft.com/webdev/2017/09/14/announcing-signalr-for-asp-net-core-2-0/中的这个示例创建一个控制台应用程序,可以将消息从简单的控制台应用程序发送到网页. 下面是控制台应用程序的简单示例,它读取用户输入并在屏幕上打
我正在使用
https://blogs.msdn.microsoft.com/webdev/2017/09/14/announcing-signalr-for-asp-net-core-2-0/中的这个示例创建一个控制台应用程序,可以将消息从简单的控制台应用程序发送到网页.
下面是控制台应用程序的简单示例,它读取用户输入并在屏幕上打印输入,我也想将相同的用户输入发送到网页. static void Main(string[] args) { while (true) { Console.WriteLine("Enter input:"); string line = Console.ReadLine(); if (line == "exit") { break; } sendSignalToClient(line); Console.Write("Message Sent To Client: " + line); } } private static void sendSignalToClient(string line) { //Send a message from this app to web client using signalR } 我刚开始学习这个.任何与此相关的材料或建议都表示赞赏. -谢谢 编辑:我正在使用示例signalR聊天应用程序. public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); } public class ChatHub : Hub { public void Send(string name,string message) { // Call the broadcastMessage method to update clients. Clients.All.InvokeAsync("broadcastMessage",name,message); } } public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddSignalR(); } public void Configure(IApplicationBuilder app,IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseFileServer(); app.UseSignalR(routes => { routes.MapHub<ChatHub>("chat"); }); } 下面是将消息发送到网页的控制台应用程序的代码 class Program : Hub { static void Main(string[] args) { Task.Run(Run).Wait(); } static async Task Run() { var connection = new HubConnectionBuilder() .WithUrl("http://localhost:59768/chat") .WithConsoleLogger() .WithMessagePackProtocol() .WithTransport(TransportType.WebSockets) .Build(); await connection.StartAsync(); Console.WriteLine("Starting connection. Press Ctrl-C to close."); var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender,a) => { a.Cancel = true; cts.Cancel(); }; connection.Closed += e => { Console.WriteLine("Connection closed with error: {0}",e); cts.Cancel(); return Task.CompletedTask; }; connection.On("broadcastMessage",async () => { }); while (true) { Thread.Sleep(2000); await connection.SendAsync("send","alex","hello"); } } 所有代码都在工作,但在控制台应用程序上我收到此异常: Microsoft.AspNetCore.Sockets.Client.HttpConnection[19] 09/17/2017 13:47:17: Connection Id 40da6d4b-9c47-4831-802f-628bbb172e10: An exception was thrown from the 'Received' event handler. System.FormatException: Target method expects 0 arguments(s) but invocation has 2 argument(s). at Microsoft.AspNetCore.SignalR.Internal.Protocol.MessagePackHubProtocol.CreateInvocationMessage(Unpacker unpacker,IInvocationBinder binder) at Microsoft.AspNetCore.SignalR.Internal.Protocol.MessagePackHubProtocol.TryParseMessages(ReadOnlyBuffer`1 input,IInvocationBinder binder,IList`1& messages) at Microsoft.AspNetCore.SignalR.Internal.HubProtocolReaderWriter.ReadMessages(Byte[] input,IList`1& messages) at Microsoft.AspNetCore.SignalR.Client.HubConnection.<OnDataReceivedAsync>d__31.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Sockets.Client.HttpConnection.<>c__DisplayClass49_0.<<ReceiveAsync>b__0>d.MoveNext() 解决方法
您的事件处理程序应该采用字符串参数,它应如下所示:
connection.On<string>("broadcastMessage",data => { }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – html页面的ASP服务器统计信息
- 在asp.net核心mvc中集成ckeditor
- MVC3中的IValidatableObject – 客户端验证
- asp.net – 如何解决“最大请求长度超出”异常?
- asp.net – 如何使用linq从datetime列获取Date
- asp.net-mvc – T4MVC:如何使用说Url.Action?
- Asp.net MVC scheduler的实现方法详解
- asp.net-mvc-3 – 对MVC4 Web API性能问题进行故障诊断
- 了解asp.net中的负载平衡
- asp.net-mvc – 在Asp MVC Telerik网格和实体框架中处理枚举