首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel不触发回调

Laravel不触发回调
EN

Stack Overflow用户
提问于 2017-09-09 16:10:41
回答 1查看 1.5K关注 0票数 1

我最近建立了我的Laravel应用程序,并配置Echo来接收推送通知。不幸的是,回调函数没有被触发,即使接收到了事件以及它的数据。您可以在下面找到我的代码:

web.php

代码语言:javascript
复制
Route::get('/pusher', function() {
    $location = ['lang' => 'langTets', 'lat' => 'latTest'];
    event(new Freight\Events\LocationUpdated($location));
    return "Event has been sent!";
});

bootstrap.js

代码语言:javascript
复制
import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');
Pusher.logToConsole = true;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'iscorrect',
    cluster: 'eu',
    encrypted: true
});

window.Echo.channel('location-updates')
    .listen('.LocationUpdated', function(location) {
        console.log("Test");
    });

我的app.blade

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>Freight</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <!-- JS -->
    <script src="{{ asset('js/app.js') }}"></script>
</head>
<body>
...

当我打开页面时,这会出现在我的控制台中:

代码语言:javascript
复制
app.js:32652 Pusher : State changed : initialized -> connecting
app.js:32652 Pusher : Connecting : {"transport":"ws","url":"censoredstuff"}
app.js:32652 Pusher : State changed : connecting -> connected with new socket ID morecensoredstuff
app.js:32652 Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"location-updates"}}
app.js:32652 Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"location-updates"}
app.js:32652 Pusher : No callbacks on location-updates for pusher:subscription_succeeded

现在,在一个触发的事件中,这是控制台中显示的:

代码语言:javascript
复制
Pusher : Event recd : {"event":"Freight\\Events\\LocationUpdated","data":{"location":{"lang":"langTets","lat":"latTest"}},"channel":"location-updates"}
app.js:32652 Pusher : No callbacks on location-updates for Freight\Events\LocationUpdated

你有什么线索吗,为什么console.log不能正常工作?如果需要更多的信息,请随时询问。

EN

回答 1

Stack Overflow用户

发布于 2017-09-10 19:41:15

我仍然没有找出问题的根源。然而,这很好地解决了这个问题:

  • 在您的事件中实现像下面这样的方法broadcastAs():

broadcastAs(){ return 'myneweventname'; }

  • 更改.js文件,以便您正在侦听新创建的事件别名(没有前导句点)。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46132582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档