内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Server-side event - how to pass parameters to server-side script? Append query string? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Server-side event - how to pass parameters to server-side script? Append query string? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

I 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?

发布评论

评论列表(0)

  1. 暂无评论