admin管理员组文章数量:1310050
I am trying to pass parameters to a PHP script that is used to stream server-side events. I am using Javascript to listen to the events. I have tried to pass parameters by appending a query string to the server-side script url:
Javascript:
var source = new EventSource("demo_sse.php?file=query");
demo_sse.php:
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data:'.$_GET["file"]."\n\n";
?>
I would expect to see "query" displayed, but instead I get no output.
I also tried:
echo 'data:'.$_SERVER['QUERY_STRING']."\n\n";
But still no output. I have verified that the script works otherwise (can print explicit strings).
I haven't found much on the subject, but I did find a couple of examples of appending the query string to the url in the EventSource argument: Server Sent Events Stop Start with new parameter.
I am trying to pass parameters to a PHP script that is used to stream server-side events. I am using Javascript to listen to the events. I have tried to pass parameters by appending a query string to the server-side script url:
Javascript:
var source = new EventSource("demo_sse.php?file=query");
demo_sse.php:
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data:'.$_GET["file"]."\n\n";
?>
I would expect to see "query" displayed, but instead I get no output.
I also tried:
echo 'data:'.$_SERVER['QUERY_STRING']."\n\n";
But still no output. I have verified that the script works otherwise (can print explicit strings).
I haven't found much on the subject, but I did find a couple of examples of appending the query string to the url in the EventSource argument: Server Sent Events Stop Start with new parameter.
Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Mar 11, 2014 at 14:41 KevinHJKevinHJ 1,1241 gold badge11 silver badges28 bronze badges 4- What could you need to pass to the server? If you're looking for bidirectional munication, use WebSockets – Ian Commented Mar 11, 2014 at 15:05
- @Ian: SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events. - quoted from html5rocks./en/tutorials/eventsource/basics - yes, I need to pass data to server to tell script where to find file. – KevinHJ Commented Mar 12, 2014 at 12:52
- @Ian: WebSockets are more plex, and require installing a module on the server. I don't need continual bidi munication, I only want to send a single bit of data upon initiation of the server-side script. WebSockets is overkill for what I need. The query string idea seemed like it ought to work, but I was looking for confirmation. I hope someone can answer the question I asked. – KevinHJ Commented Mar 12, 2014 at 13:17
- why not just ajax a post request? is there something I'm missing here? – Royalty Commented Jun 10, 2015 at 21:16
1 Answer
Reset to default 5I have tried the following code and SSE seemed to work:
var es = new EventSource('http://localhost/ssed.php?asdf=test111');
es.onmessage=function(e){console.log(e.data);}
ssed.php:
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data:'.$_GET['asdf']."\n\n";
How is your onmessage function written? Is it possible that your browser doesn't support SSE?
版权声明:本文标题:javascript - Server-side event - how to pass parameters to server-side script? Append query string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741857921a2401454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论