简体   繁体   English

服务器发送事件的自定义事件

[英]custom events with server sent events

I am trying to utilize server sent events to handle the client side of some async operations im doing server side. 我正在尝试利用服务器发送的事件来处理服务器端执行的一些异步操作的客户端。 Scroll to the bottom to see the final question. 滚动到底部以查看最后一个问题。

What I have is an web app that needs a global search. 我拥有的是一个需要全局搜索的Web应用程序。 The returns can be massive and take some time as they are actually sent to a webservice and then returned as XML. 返回值可能是巨大的,并且需要花费一些时间,因为它们实际上被发送到Web服务,然后以XML返回。

I am trying to use server sent events (javascript: EventSource) to enable this. 我正在尝试使用服务器发送的事件(javascript:EventSource)来启用此功能。 It works great for just one event but if I want to do a bunch of different events it gets a little weird, at least for me. 它仅适用于一个事件,但是如果我想执行许多不同的事件,至少对于我来说有点奇怪。 I am new to SSE. 我是SSE的新手。

so far I have done this: 到目前为止,我已经做到了:

var sse = new EventSource('testsse.aspx');

function init() {
            sse.onopen = function (e) {
                console.log(e.data);
            };

            sse.onmessage = function (e) {
//if data has been returned the "data:" field will not be empty so we know the server operation is complete. 
                if (e.data.length>0) {
                    $('#rgCcr').kendoGrid({
                        dataSource: JSON.parse(e.data)
                    });
                    sse.close();
                }
                    console.log("message: " + e.data);
            };

$(document).ready(function() {
    init();
});

on the server I have an aspx page that is building the json strings. 在服务器上,我有一个正在构建json字符串的aspx页面。 as I mentioned I have it working great if I just use the standard "data: my message\\n\\n" format. 正如我提到的,如果我仅使用标准的“数据:我的邮件\\ n \\ n”格式,则效果很好。 What I need to do is get a few more pieces of info in there. 我需要做的是在那里获取更多信息。

The spec for SSE says that you can specify an event to fire. SSE规范指出,您可以指定要触发的事件。 I cant get this to work so far though. 我到目前为止不能使它工作。

in my aspx I am building the string like this: 在我的aspx中,我正在构建像这样的字符串:

json = string.Format("id:{0}\n event:myEvent\n data:{1}\n\n", id, ser.Serialize(ccrData));

I have tried creating a custom event but I am not sure what to bind it to in order to catch it. 我尝试创建一个自定义事件,但不确定将其绑定到什么以捕获该事件。

On the page I have 5 different areas that are being search as part of the global search. 在页面上,我有5个不同的区域正在作为全局搜索的一部分进行搜索。 Each is executed as an async task on the server and as they complete they will send the data back to the client. 每个任务都作为异步任务在服务器上执行,完成后,它们会将数据发送回客户端。 once the client detects the event has submitted data it will close the listener and add the data results to the kendo grid. 一旦客户端检测到事件已提交数据,它将关闭侦听器并将数据结果添加到kendo网格。

How can I use the event field of the SSE to tell the difference between what is coming back? 如何使用SSE的事件字段来区分返回结果之间的区别?

Rather than using the "onmessage" function, you could try registering an event listener. 您可以尝试注册事件侦听器,而不是使用“ onmessage”功能。 That way you can choose to listen to a particular event type. 这样,您可以选择监听特定的事件类型。

es = new EventSource('/events', withCredentials: true);

es.addEventListener("open", function (e) {
  alert("Connecting...");
});

es.addEventListener("error", function (e) {
  alert("Error");
});

es.addEventListener("myEvent", function (e) {
  alert("MyEvent");
  alert(e.data);
});

There's great documentation here: http://html5doctor.com/server-sent-events/ 这里有很棒的文档: http : //html5doctor.com/server-sent-events/

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

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