Moh*_*hif 3 javascript php laravel pusher
我正在尝试测试一个基本的推送器触发事件,它给我一个简单的警报和一个控制台日志,但它不起作用。从推送器控制台调试收到响应,但没有警报。我正在使用 laravel 5.3 及其路线和视图。下面是我的代码。我已经审查了敏感信息。
路由文件 web.php
Route::get('/bridge', function() {
error_reporting(E_ALL);
$options = array(
'cluster' => 'ap1',
'encrypted' => true
);
$pusher = new Pusher(
'key censored',
'secret censored',
'app id censore',
$options
);
$data['message'] = 'hello world';
$pusher->trigger('test_channel', 'my_event', $data);
return view('pusher');
});
Run Code Online (Sandbox Code Playgroud)
并查看 pusher.blade.php
<!DOCTYPE html>
<head>
<title>Pusher Test</title>
<script src="https://js.pusher.com/3.2/pusher.min.js"></script>
<script>
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher('e5bbf707214a6223d044', {
cluster: 'ap1',
encrypted: true
});
var channel = pusher.subscribe('test_channel');
channel.bind('my_event', function(data) {
alert(data);
console.log(data);
});
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
chrome 控制台给了我以下日志。
Pusher : State changed : initialized -> connecting
Pusher : Connecting : {"transport":"ws","url":"wss://ws-ap1.pusher.com:443/app/censored key?protocol=7&client=js&version=3.2.2&flash=false"}
Pusher : State changed : connecting -> connected with new socket ID 5034.8700909
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"test_channel"}}
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"test_channel"}
Pusher : No callbacks on test_channel for pusher:subscription_succeeded
Run Code Online (Sandbox Code Playgroud)
您为my_event事件创建了绑定。此错误是抱怨您没有该pusher:subscription_succeeded事件的回调。如果你想捕捉并处理它,你需要创建一个绑定。
https://pusher.com/docs/client_api_guide/client_presence_channels#pusher-subscription-succeeded
channel.bind('pusher:subscription_succeeded', function(members) {
alert('successfully subscribed!');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8232 次 |
| 最近记录: |