从 web->web api->console app 再返回的最佳通信方式是什么? [英] What is the best way of communicating from web->web api->console app and back again?

查看:35
本文介绍了从 web->web api->console app 再返回的最佳通信方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个小代码项目以从该过程中学习,但我不确定在不同部分之间进行交流的最佳方式是什么.

I'm starting up a little code project to learn from the process, but I am not sure what's the best way of communicating between the different parts.

  • 首先,我有一个纯 html/js 客户端,用户需要经常在其中获取实时更新.
  • 其次,我正在考虑运行一个 Web API 应用程序来提供数据.
  • 第三,我有一个控制台应用程序正在运行,它需要与 web api 应用程序进行通信.

所以我正在考虑从客户端-> web api->控制台应用程序一直使用 WebSockets,但我无法让它工作.我可以让控制台应用程序作为服务器工作,但我不知道如何让 web api 作为客户端工作,这样当它启动时,创建到控制台应用程序的连接,并保持它打开以进行通信,同时根据请求将数据传递给客户端.

So I'm thinking about using WebSockets all the way from client->web api->console app, but I have trouble making it work. I can make the console app work as a server, but I can't figure out how to make the web api work as a client, so that when it spins up, creates a connection to the console app, and keeps it open for communication, while it delivers data to the clients upon requests.

我尝试了不同的实现,最终使用了 SignalR,因为这似乎就是今天人们使用的 :)

I tried with different implementations and I have ended up with SignalR, as it seems like that's what people use today :)

既然我在建立连接时遇到了所有这些问题,我想知道是否有更好的方法来发送满足我的要求的数据?如果 WebSocket(使用 SignalR)是可行的方法,您能否提供一些包含工作示例的链接?我已经尝试了所有来自 Google 的顶级链接,但都没有成功^^

Since I have all these problems getting a connection I am wondering if there are better ways of sending data that fulfills my requirements? If WebSocket (using SignalR) is the way to go, can you provide some links with working examples? I have tried all the top links from Google with no success ^^

提前致谢

推荐答案

您的 WebAPI 项目也可以充当 WebSocket 服务器.检查 此链接 使用 IHttpHandler,但您也可以在 WebAIP 的控制器中执行此操作,详情如下:将 WebSockets 与 ASP.NET Web API 结合使用

Your WebAPI project can act as WebSocket server as well. Check this link that uses an IHttpHandler, but you can also do it in a WebAIP's controller how is explained here : Using WebSockets with ASP.NET Web API

控制台应用程序应作为客户端连接,例如使用 ClientWebSocket 类.

The console application should connect as client, using for example ClientWebSocket class.

WebSockets 是持久的全双工连接,因此一旦客户端连接,两端都可以将信息推送到另一端.

WebSockets are persistent full duplex connections, so once the client is connected both ends can push information to the other end.

这篇关于从 web->web api->console app 再返回的最佳通信方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆