gpt4 book ai didi

javascript - 从 Windows 服务 C# 调用 javascript 函数

转载 作者:行者123 更新时间:2023-11-28 00:51:55 25 4
gpt4 key购买 nike

我需要从用 C# 编写的 Windows 服务调用 chrome-window 内的 javascript 函数。

浏览器完全由我使用,因此我可以配置没有问题。

例如,windows服务是一个文件检查器,当某个文件被更改时,必须弹出一个js警报。

-编辑-

以下内容适用于客户端到客户端的通信(服务器端代码)。因此,当服务器上发生特定事件时,我可以在客户端上显示该事件(我希望评论能做到这一点)

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using SignalR.Hosting.Self;
using SignalR.Hubs;

namespace Net.SignalR.SelfHost
{

class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8081/";

var server = new Server(url);
server.MapHubs();

server.Start();
Console.WriteLine("SignalR server started at " + url);


Console.ReadLine();

// Clients[collection].flush("bericht: " + message + collection);

}

//public void PushMessage(string bericht)
//{
// var hubConnection = new HubConnection("http://localhost:8081/");
// var serverHub = hubConnection.CreateProxy("CollectionHub");

// serverHub.On("flush", message => System.Console.WriteLine(message));
// hubConnection.Start().Wait();
// serverHub.Invoke("Subscribe", "Product");
// string line = null;
// while ((line = bericht) != null)
// {
// serverHub.Invoke("Publish", line, "Product").Wait();
// }

// System.Console.Read();
//}



public class CollectionHub : Hub
{

public void Subscribe(string collectionName)
{
Groups.Add(Context.ConnectionId, collectionName);
Console.WriteLine("Subscribed to: " + collectionName);
//serverHub.Invoke("Publish", "dit is een eerste test", "Product").Wait();



}

public Task Unsubscribe(string collectionName)
{
return Clients[collectionName].leave(Context.ConnectionId);
}

public void Publish(string message, string collection)
{
Clients[collection].flush("bericht: " + message + collection);
}
}

}
}

最佳答案

听起来你正在描述 SignalR .

What is ASP.NET SignalR?

ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

You may have heard of WebSockets, a new HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fallback to other techniques and technologies when it isn't, while your application code stays the same.

SignalR also provides a very simple, high-level API for doing server to client RPC (call JavaScript functions in your clients' browsers from server-side .NET code) in your ASP.NET application, as well as adding useful hooks for connection management, e.g. connect/disconnect events, grouping connections, authorization.

What can you do with ASP.NET SignalR?

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements Ajax long polling to retrieve new data, is candidate for using SignalR.

它的基本功能是让您可以双向访问客户端和服务器端功能,可以在 asp.net website 上找到其用法的简单示例。这将使您很好地了解如何使用它以及它的功能。

关于javascript - 从 Windows 服务 C# 调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26670096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com