将“实时”控制台输出发送到客户端浏览器的技术

时间:2022-01-04 03:55:06

I have a .NET console application that I want to start on the server of an ASP.NET MVC application. It produces output continuously for a certain time and I want to intercept this output and show it to the client in his browser window.

我有一个.NET控制台应用程序,我想在ASP.NET MVC应用程序的服务器上启动。它会连续产生一段时间的输出,我想拦截这个输出并在浏览器窗口中显示给客户端。

From another console application, I can do it like this:

从另一个控制台应用程序,我可以这样做:

public static void Main(string[] args)
{
  Process process = new Process();
  process.StartInfo.FileName = "RandomOutputCreator.exe";
  process.StartInfo.UseShellExecute = false;
  process.StartInfo.RedirectStandardOutput = true;
  process.OutputDataReceived += (sender, e) =>
  {
    Console.WriteLine(e.Data);
  };

  process.Start();
  process.BeginOutputReadLine();

  Console.ReadKey();
}

The problem is that in the MVC application, I can't push the data I read to the client, but rather rely on requests to the controller to get my data. Something like WebSockets could maybe help me here, but I'm new to this kind of thing and wonder if there might be a "built-in" way to accomplish this.

问题是在MVC应用程序中,我不能将我读取的数据推送到客户端,而是依赖于对控制器的请求来获取我的数据。像WebSockets这样的东西可能对我有所帮助,但我对这种事情不熟悉,并想知道是否有一种“内置”的方法来实现这一目标。

Then there's the Web API thing. Could this be of use perhaps, since it seems to go well with MVC?

然后是Web API的事情。这可能是有用的,因为它似乎与MVC相处得很好?

(Since I do not know what a fitting technology might be, please excuse the lack of tags and feel free to fill some in that you think fit).

(因为我不知道什么是合适的技术,请原谅标签的缺失,并随意填写你认为合适的一些)。

3 个解决方案

#1


3  

This topic typically reminds me of a tutorial I followed in order to allow real-time communication from my browser to an ASP.NET application.

本主题通常会让我想起我所遵循的教程,以便允许从浏览器到ASP.NET应用程序的实时通信。

In summary : What you're looking for are indeed WebSocket, and there is no standard built-in functions to handle that. But, in order to help you doing some stuff, you still have the library signalR!

总结:您正在寻找的确实是WebSocket,并且没有标准的内置函数来处理它。但是,为了帮助你做一些事情,你仍然有图书馆信号R!

Here's the link to the referenced tutorial : http://www.asp.net/signalr/overview/getting-started/real-time-web-applications-with-signalr

以下是参考教程的链接:http://www.asp.net/signalr/overview/getting-started/real-time-web-applications-with-signalr

#2


1  

You can try "print" console output in a separate frame (see iframe HTML tag). You should set one of your actions as a source (URL) of the frame. You'll need to configure the IIS to run this action without execution time limit.

您可以在单独的框架中尝试“打印”控制台输出(请参阅iframe HTML标记)。您应将其中一个操作设置为框架的源(URL)。您需要配置IIS以在没有执行时间限制的情况下运行此操作。

Next, your action should run an external program, intercept its output, and write it to HTTP output (see ContentResult).

接下来,您的操作应该运行外部程序,拦截其输出,并将其写入HTTP输出(请参阅ContentResult)。

#3


1  

I have a small project that does exactly that: https://github.com/vtortola/WebSocketListener/wiki/WebSocketListener-Terminal-Server

我有一个小项目就是这样做的:https://github.com/vtortola/WebSocketListener/wiki/WebSocketListener-Terminal-Server

将“实时”控制台输出发送到客户端浏览器的技术

Give it a look, it may give you some ideas.

看看它,它可能会给你一些想法。

#1


3  

This topic typically reminds me of a tutorial I followed in order to allow real-time communication from my browser to an ASP.NET application.

本主题通常会让我想起我所遵循的教程,以便允许从浏览器到ASP.NET应用程序的实时通信。

In summary : What you're looking for are indeed WebSocket, and there is no standard built-in functions to handle that. But, in order to help you doing some stuff, you still have the library signalR!

总结:您正在寻找的确实是WebSocket,并且没有标准的内置函数来处理它。但是,为了帮助你做一些事情,你仍然有图书馆信号R!

Here's the link to the referenced tutorial : http://www.asp.net/signalr/overview/getting-started/real-time-web-applications-with-signalr

以下是参考教程的链接:http://www.asp.net/signalr/overview/getting-started/real-time-web-applications-with-signalr

#2


1  

You can try "print" console output in a separate frame (see iframe HTML tag). You should set one of your actions as a source (URL) of the frame. You'll need to configure the IIS to run this action without execution time limit.

您可以在单独的框架中尝试“打印”控制台输出(请参阅iframe HTML标记)。您应将其中一个操作设置为框架的源(URL)。您需要配置IIS以在没有执行时间限制的情况下运行此操作。

Next, your action should run an external program, intercept its output, and write it to HTTP output (see ContentResult).

接下来,您的操作应该运行外部程序,拦截其输出,并将其写入HTTP输出(请参阅ContentResult)。

#3


1  

I have a small project that does exactly that: https://github.com/vtortola/WebSocketListener/wiki/WebSocketListener-Terminal-Server

我有一个小项目就是这样做的:https://github.com/vtortola/WebSocketListener/wiki/WebSocketListener-Terminal-Server

将“实时”控制台输出发送到客户端浏览器的技术

Give it a look, it may give you some ideas.

看看它,它可能会给你一些想法。