如何使用 signalR 核心从 asp.net 核心接收消息到 UWP
How to receive message from the asp.net core using signalR core to UWP
SignalR 核心是使用 javascript 客户端或 Angular 进行演示
我的案例是使用 UWP 渲染前端。虽然微软只告诉如何调用从客户端到服务器的消息,但它的文档没有显示如何接收消息
[https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2][1]
这是我的服务器:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IInventoryServices, InventoryServices>();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseSignalR(route =>
{
route.MapHub<MessageHub>("/hub");
});
app.UseMvc();
}
}
这是控制器:
[Route("api/hub")]
[ApiController]
public class MessController : Controller
{
private IHubContext<MessageHub> _messhubContext;
public MessController(IHubContext<MessageHub> messhubContext)
{
_messhubContext = messhubContext;
}
public ActionResult Post()
{
_messhubContext.Clients.All.SendAsync("send", "Strypper", "Howdy");
System.Diagnostics.Debug.WriteLine("I'm here");
return Ok();
}
这里是中心:
public class MessageHub : Hub
{
public Task Send(string user ,string message)
{
return Clients.All.SendAsync("Send", user, message);
}
}
我的 "PostMan" 搞砸了,我不想讨论它。这里有使用 uwp 框架的人可以告诉我如何从我制作的服务器接收消息吗?
不好意思,原来是我理解错了,转过来了
对于服务器到客户端的通信,您必须遵循 documentation here。
您需要像这样在 UWP 中定义一个侦听器:
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
//do something
});
然后像这样在服务器端发送消息:
await Clients.All.SendAsync("ReceiveMessage", user,message);
上一个回答
要从客户端调用 Hub
方法,您可以使用 InvokeAsync
方法:
await connection.InvokeAsync("MyMethod", "someparameter");
然后您只需在 Hub
class
中创建方法
public class MessageHub : Hub
{
public Task Send(string user ,string message)
{
return Clients.All.SendAsync("Send", user, message);
}
public Task MyMethod(string parameter)
{
//do something here
}
}
还有一个 InvokeAsync<TResult>
的重载,允许您创建具有 return 类型的方法。
SignalR 核心是使用 javascript 客户端或 Angular 进行演示 我的案例是使用 UWP 渲染前端。虽然微软只告诉如何调用从客户端到服务器的消息,但它的文档没有显示如何接收消息 [https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2][1]
这是我的服务器:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IInventoryServices, InventoryServices>();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseSignalR(route =>
{
route.MapHub<MessageHub>("/hub");
});
app.UseMvc();
}
}
这是控制器:
[Route("api/hub")]
[ApiController]
public class MessController : Controller
{
private IHubContext<MessageHub> _messhubContext;
public MessController(IHubContext<MessageHub> messhubContext)
{
_messhubContext = messhubContext;
}
public ActionResult Post()
{
_messhubContext.Clients.All.SendAsync("send", "Strypper", "Howdy");
System.Diagnostics.Debug.WriteLine("I'm here");
return Ok();
}
这里是中心:
public class MessageHub : Hub
{
public Task Send(string user ,string message)
{
return Clients.All.SendAsync("Send", user, message);
}
}
我的 "PostMan" 搞砸了,我不想讨论它。这里有使用 uwp 框架的人可以告诉我如何从我制作的服务器接收消息吗?
不好意思,原来是我理解错了,转过来了
对于服务器到客户端的通信,您必须遵循 documentation here。
您需要像这样在 UWP 中定义一个侦听器:
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
//do something
});
然后像这样在服务器端发送消息:
await Clients.All.SendAsync("ReceiveMessage", user,message);
上一个回答
要从客户端调用 Hub
方法,您可以使用 InvokeAsync
方法:
await connection.InvokeAsync("MyMethod", "someparameter");
然后您只需在 Hub
class
public class MessageHub : Hub
{
public Task Send(string user ,string message)
{
return Clients.All.SendAsync("Send", user, message);
}
public Task MyMethod(string parameter)
{
//do something here
}
}
还有一个 InvokeAsync<TResult>
的重载,允许您创建具有 return 类型的方法。