首页 > 解决方案 > 将数据从 NodeJS 服务器流式传输到 .NET CORE 的方式?

问题描述

对不起,因为我是 .NET 和 Visual Studio 的新手:

我想将数据从我的 NodeJS 服务器 (Visual Studio Code) 流式传输到我的 .NET Core 应用程序 (Visual Studio)。

我以前在这里发过帖子,但由于已经发布了答案而将其删除,但是我研究了很多,但找不到任何答案。

所以我目前的工作方式是将我的 NodeJS 设置为写入文件,然后我的 .NET 同时从文件中读取。正如您可能会说的那样,这远非理想,因为它们相互冲突并且经常出错,这意味着流远非完美。

然后我发现这些东西叫做管道,有没有办法在 .NET 或 NodeJS 上创建一个“管道”服务器并在另一个框架中连接到它,以便在两者之间轻松传递数据?

如果您需要更多说明,请告诉我,谢谢。

标签: javascriptc#node.js.net

解决方案


您可以创建 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


推荐阅读