javascript - 将数据从 NodeJS 服务器流式传输到 .NET CORE 的方式?
问题描述
对不起,因为我是 .NET 和 Visual Studio 的新手:
我想将数据从我的 NodeJS 服务器 (Visual Studio Code) 流式传输到我的 .NET Core 应用程序 (Visual Studio)。
我以前在这里发过帖子,但由于已经发布了答案而将其删除,但是我研究了很多,但找不到任何答案。
所以我目前的工作方式是将我的 NodeJS 设置为写入文件,然后我的 .NET 同时从文件中读取。正如您可能会说的那样,这远非理想,因为它们相互冲突并且经常出错,这意味着流远非完美。
然后我发现这些东西叫做管道,有没有办法在 .NET 或 NodeJS 上创建一个“管道”服务器并在另一个框架中连接到它,以便在两者之间轻松传递数据?
如果您需要更多说明,请告诉我,谢谢。
解决方案
您可以创建 localhost RestAPI
import express from 'express';
const app = express();
app.post('/', (req, res) => {
return res.send('Received a POST HTTP method');
});
app.listen(8080) //port
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
namespace RestAPI
{
public class WebServer
{
private readonly HttpListener _listener = new HttpListener();
private readonly Func<HttpListenerRequest, string> _responderMethod;
public WebServer(IReadOnlyCollection<string> prefixes, Func<HttpListenerRequest, string> method)
{
if (!HttpListener.IsSupported)
{
throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
}
// URI prefixes are required eg: "http://localhost:8080/test/"
if (prefixes == null || prefixes.Count == 0)
{
throw new ArgumentException("URI prefixes are required");
}
if (method == null)
{
throw new ArgumentException("responder method required");
}
foreach (var s in prefixes)
{
_listener.Prefixes.Add(s);
}
_responderMethod = method;
_listener.Start();
}
public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
: this(prefixes, method)
{
}
public void Run()
{
ThreadPool.QueueUserWorkItem(o =>
{
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem(c =>
{
var ctx = c as HttpListenerContext;
try
{
if (ctx == null)
{
return;
}
var rstr = _responderMethod(ctx.Request);
var buf = Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64 = buf.Length;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
string cmd = ctx.Request.RawUrl;
if (cmd.Contains("cmd="))
{
cmd = cmd.Split('=')[1];
cmd = HttpUtility.UrlDecode(cmd);
Debug.WriteLine($"Request : {cmd}");
Program.MakeCMD(cmd);
}
}
catch
{
// ignored
}
finally
{
// always close the stream
if (ctx != null)
{
ctx.Response.OutputStream.Close();
}
}
}, _listener.GetContext());
}
}
catch (Exception ex)
{
// ignored
}
});
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
}
public static class Program
{
public static string prefix = "http://localhost:8081";
public static string SendResponse(HttpListenerRequest request)
{
string txt = ""; //You can return some text/json
return txt;
}
public static void Main(string[] args)
{
try
{
var ws = new WebServer(SendResponse, prefix);
ws.Run();
}
catch
{
ConsoleLog.WriteLine("It looks like you are not running in administrator mode. Administrator Mode is required to start the web server!");
}
}
}
}
我没有找到更简单的 c# 示例:)
然后在节点js中使用XMLHttpRequest,在c#中使用HttpClient
推荐阅读
- reactjs - 如何在反应中使用redux从api获取数据
- json - 在 Scala Spark 中将数据帧拆分为多个数据帧
- r - 基于匹配字符串模式的过滤
- sql - 如何识别查询/存储过程中使用的所有表
- javascript - 来自字符串 + 变量的 Javascript 变量
- azure - 在 Asp.Net CORE 3.x 中实现 Active Directory 组
- javascript - JSONLint 说 JSON 有效,但我无法在 JavaScript 中从中获取值
- python - 如何打印以下程序的目标总和的不同对?
- reactjs - 为什么这个流类型定义不起作用?
- ios - Siri Intent Extension 中的 TouchID 身份验证