简体   繁体   English

从Windows Service C#调用JavaScript函数

[英]Call javascript function from windows service c#

I need to call a javascript function inside a chrome-window from a windows service written in c#. 我需要从用C#编写的Windows服务的Chrome浏览器窗口中调用javascript函数。

The browser is entirely at my disposal so I can configuration is no problem. 浏览器完全由我支配,因此我可以进行配置没有问题。

For example, the windows service is a file checker, when a certain file is changed there has to popup a js alert. 例如,Windows服务是一个文件检查器,当某个文件被更改时,必须弹出一个js警报。

-EDIT- -编辑-

The following works fine for client to client communication (server-side code). 以下内容适用于客户端到客户端的通信(服务器端代码)。 So when a specific event happens on the server I can display this on the client (I hoped commented would do that) 因此,当服务器上发生特定事件时,我可以在客户端上显示该事件(我希望评论能够做到这一点)

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);
            }
        }

    }
}

Sounds like you are describing SignalR . 听起来您正在描述SignalR

What is ASP.NET SignalR? 什么是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. ASP.NET SignalR是面向ASP.NET开发人员的新库,它使向应用程序中添加实时Web功能变得异常简单。 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. 您可能听说过WebSockets,这是一个新的HTML5 API,可在浏览器和服务器之间进行双向通信。 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将在可用的情况下使用WebSockets,并在不可用时优雅地回退到其他技术和技术,而您的应用程序代码保持不变。

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, eg connect/disconnect events, grouping connections, authorization. SignalR还提供了一个非常简单的高级API,用于在ASP.NET应用程序中执行服务器到客户端RPC(从服务器端.NET代码在客户端浏览器中调用JavaScript函数),并添加了用于连接管理的有用钩子,例如连接/断开连接事件,分组连接,授权。

What can you do with ASP.NET SignalR? 您可以使用ASP.NET SignalR做什么?

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. SignalR可用于将任何类型的“实时” Web功能添加到ASP.NET应用程序。 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. 每当用户刷新网页以查看新数据,或者该页面实施Ajax长轮询以检索新数据时,都可以使用SignalR。

What it basicly does, is giving you access to the client AND server side functions in both directions, a simple example of it's usage can be found on the asp.net website which will give you a good idea on how to use it and what it's capable of doing. 它的基本作用是使您可以双向访问客户端和服务器端功能,可以在asp.net网站上找到有关其用法的简单示例,这将为您提供有关如何使用它以及它是什么的一个好主意。有能力做。

You want to be using something like signal IR to do that, its what its designed for, essentially you are describing real time functionality; 您想使用诸如信号IR之类的东西来实现它的目的,实际上,您正在描述实时功能。

Signal IR can be found here and has a great section on javascript in its wiki 可以在此处找到Signal IR 并且在其Wiki中有关于javascript的精彩部分

In particular you probably want to take a look at Hubs 特别是您可能想看看Hubs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM