服务器发送事件停止以新参数开始 [英] Server Sent Events Stop Start with new parameter

查看:57
本文介绍了服务器发送事件停止以新参数开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙.

我正在使用服务器发送事件根据存储在数据库中的数据动态更新网站.

I'm using Server Sent Events to dynamically update a website based on data stored in a database.

我现在希望根据上一条消息中收到的数据将新参数 ('abc.php/?lastID=xxx') 传递回 PHP 脚本.

I now wish to pass a new parameter ('abc.php/?lastID=xxx') back to the PHP script based on data received in the previous message.

我知道我可以使用 event.close 来停止当前的流",但我正在努力弄清楚如何为每个新的 EventSource 使用相同的 'EventListener'URL是动态创建的

I understand i can use event.close to stop the current 'stream' but im struggling to work out how to use the same 'EventListener' for each new EventSource where the URL is dynamically created

有人能指出我正确的方向吗?

Can somebody point me in the right direction please?

谢谢,

推荐答案

Ok 花了一些时间来更好地掌握 Javascript.

Ok spent some time pretty much getting better grip on Javascript.

var source = new EventSource("xyz.php?last=0");
source.addEventListener('message', onMessageHandler,false);

function onMessageHandler(event) {
   console.log(event.data);//just for debug purposes

   event.target.close(); //close current stream.
   var last = "1234"; // loaded from event.data in my solution.
   var sourceNew = new EventSource("xyz.php?last=" + last); // start another 
   sourceNew.addEventListener('message', onMessageHandler, false); //event

}

我似乎已经解决了我遇到的问题.

and i seem to have solved the problem i had.

这篇关于服务器发送事件停止以新参数开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆