使用js更新代码以实时监听,但仍然是没有任何反应的相同问题。
我正在努力实现以下目标:
1)'Eric'在文件 eric.php中发送包含以下代码的Feed:
<?php
require_once 'PATH/TO/MY/vendor/autoload.php';
$client = new GetStream\Stream\Client('mykey', 'mysecret');
// For the feed group 'user' and user id 'eric' get the feed
$ericFeed = $client->feed('user', 'eric');
// Add the activity to the feed
$data = [
"actor"=>"eric",
"verb"=>"like",
"object"=>"3",
"tweet"=>"Hello world"
];
$ericFeed->addActivity($data);
?>
另外,我可以使用JS代替,但这也不能解决问题:
<script>
// Initialize the client with your api key, no secret and your app id
var client = stream.connect('key', null, 'id');
// For the feed group 'user' and user id 'eric' get the feed
// The access token at the end is only needed for client side integrations
var ericFeed = client.feed('user', 'eric', 'key');
// Add the activity to the feed
ericFeed.addActivity({
actor: 'eric',
tweet: 'Hello world',
verb: 'tweet',
object: 1
});
</script>
2)杰西卡登录她的页面 jessica.php 希望收到有关Eri饲料的通知。在本教程之后,这是我用来实现这一目的的:
<?php
//load Pusher or stream libraries
require_once '../../../vendor/autoload.php';
$client = new GetStream\Stream\Client('mykey','mysecret');
$client = new GetStream\Stream\Client('b5qhgudtn6my', '4qkfwmsvfrprm3zp5smuxfnvrcb227f8sf49pt7mene3ra8kmz2mgk3tkne4nync');
// For the feed group 'user' and user id 'eric' get the feed
//Stream
$jessicaFlatFeed = $client->feed('timeline', 'jessica');
$jessicaFlatFeed->followFeed('user', 'eric');
$response = $jessicaFlatFeed->getActivities(0, 3);
$ericFeed = $client->feed('user', 'eric');
$token = $ericFeed->getToken();
?>
<div class="12u 12u align-center">
<h3>RECEIVE UPDATE HERE</h3>
<script type="text/javascript" src="/stream-js-master/bower_components/getstream/dist/js/getstream.js"></script>
<script type="text/javascript">
var client = stream.connect('b5qhgudtn6my', null, '19328');
// Javascript client side feed initialization
var ericFeed = client.feed("user", "eric", "<?php echo $token;?>");
// Listen to feed changes in realtime
var promise = ericFeed.subscribe(function(data){
alert("WORKING!");
console.log("Working!!!", data);
});
// Add an activity when the websocket is ready
promise.then(function() {
ericFeed.addActivity({actor:"eric", verb: "tweet", object: 2, tweet: "AWESOME!"});
});
</script>
</script>
</div>
此时我希望在Jessica的页面中弹出警告消息,但事实并非如此。
下面我将在
中报告教程中的代码 https://getstream.io/get_started/?language=php
步骤2/5 php:Flat feed:
// See https://github.com/tbarbugli/stream-php for install instructions
// Initialize the client with your api key and secret
$client = new GetStream\Stream\Client('key', 'secret');
// For the feed group 'user' and user id 'eric' get the feed
$ericFeed = $client->feed('user', 'eric');
// Add the activity to the feed
$data = [
"actor"=>"eric",
"verb"=>"like",
"object"=>"3",
"tweet"=>"Hello world"
];
$ericFeed->addActivity($data);
注意:这段代码是我在我自己的代码的eric.php中放置的。
3/5:php:关注
// Let Jessica's flat feed follow Eric's feed
$jessicaFlatFeed = $client->feed('timeline', 'jessica');
$jessicaFlatFeed->followFeed('user', 'eric');
注意:在调用JS代码之前,我将它放在jessica.php的顶部。
5/5:php:实时(4/5是我现在不需要的聚合Feed)
// Listening to realtime updates is only available in JS
// You can pass the feed token as follows
// Generating tokens for client side usage
$token = **$user1**->getToken();
// Javascript client side feed initialization
// ericFeed = client.feed('user:eric', '{{ token }}');
注意:我将 $ user1 替换为 $ eric 。 这部分代码放在 eric.php 中,但是,我不清楚我需要做什么
ericFeed = client.feed('user:eric', '{{ token }}');
在本教程的第5/5页中已注释掉。
此时教程中没有更多代码。
答案 0 :(得分:2)
已解决:我报告了 eric.php 和 jessica.php
的工作代码eric.php (创建Feed的人)
<?php
//Stream: create feed
require_once '../../../vendor/autoload.php';
$client = new GetStream\Stream\Client('MY_KEY', 'MY_SECRET');
// For the feed group 'user' and user id 'eric' get the feed
$ericFeed = $client->feed('user', 'eric');
// Add the activity to the feed
$data = [
"actor"=>"eric",
"verb"=>"like",
"object"=>"3",
"tweet"=>"Hello world"
];
$ericFeed->addActivity($data);
//End stream: create feed
?>
jessica.php:(收到Feed的人)
<?php
require_once '../../../vendor/autoload.php';
$client = new GetStream\Stream\Client('MY_KEY', 'MY_SECRET');
//Stream
$ericFeed = $client->feed('user', 'eric');
$token = $ericFeed->getToken();
//Get stream
?>
<script type="text/javascript" src="path/to/getstream.js"></script>
<script type="text/javascript">
var client = stream.connect('MY_KEY', null, 'APP_ID');
var ericFeed = client.feed("user", "eric", "<?php echo $token?>");
function callback(data) {
alert("New Feed Received!");
console.log("New Feed received!!!", data);
}
function successCallback(data) {
console.log('now listening to changes in realtime');
}
function failCallback(data) {
alert('something went wrong, check the console logs');
console.log(data);
}
ericFeed.subscribe(callback).then(successCallback, failCallback);
</script>