标签: laravel-echo

使用 Laravel Websockets 绑定 Laravel Echo 上的回调

我想知道我的用户何时出现连接错误或连接断开。我读过很多文章说这是可能的,但我一无所获,我开始认为这可能是我正在使用的第三方(例如https://github.com/beyondcode/laravel-websockets )的问题,有人遇到过这样的问题吗?

以下根本不产生任何日志。

window.Echo.connector.pusher.bind('reconnect', (channel, data) => {
    console.log('RE-CONNECTED');
});

window.Echo.connector.pusher.bind('reconnecting', (channel, data) => {
    console.log('re-connecting...');
});

window.Echo.connector.pusher.bind('reconnect_error', (channel, data) => {
    console.log('reconnection error!');
});

window.Echo.connector.pusher.bind('reconnect_attempt', (channel, data) => {
    console.log('re-connecting...');
});

window.Echo.connector.pusher.bind('connect_error', (channel, data) => {
    console.log('connect error...');
});

window.Echo.connector.pusher.bind('error', (channel, data) => {
    console.log('error...');
});

window.Echo.connector.pusher.bind_global((channel, data) => {
    console.log(channel, data);
});
Run Code Online (Sandbox Code Playgroud)

如果我使用建议的类似内容

window.Echo.connector.socket.on('connect_error', function(){
    console.log('connected', window.Echo.socketId());
});

window.Echo.connector.socket.on('connect', function(){
    console.log('connected', window.Echo.socketId());
});

window.Echo.connector.socket.on('disconnect', function(){
    console.log('disconnected');
});

window.Echo.connector.socket.on('reconnecting', function(attemptNumber){
    console.log('reconnecting', attemptNumber);
});
Run Code Online (Sandbox Code Playgroud)

我明白了Uncaught TypeError: Cannot read …

laravel-echo laravel-websockets

4
推荐指数
1
解决办法
3735
查看次数

私人频道无法与 Laravel echo 服务器一起使用

我在控制台上收到此 JS 错误:

app.js:167 Uncaught ReferenceError:receiverId 未定义

这是我的完整代码:

私人聊天控制器:

event(new PrivateMessageEvent($chat, $receiverId));
Run Code Online (Sandbox Code Playgroud)

私信活动:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
use App\PrivateChat;

class PrivateMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $privateChat, $receiverId;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(PrivateChat $privateChat, $receiverId)
    {
        $this->privateChat = $privateChat;
        $this->receiverId = $receiverId;
    }

    /**
     * Get the channels the event should broadcast on. …
Run Code Online (Sandbox Code Playgroud)

websocket socket.io laravel laravel-echo laravel-5.4

3
推荐指数
1
解决办法
1万
查看次数

在没有 Vue.js 的情况下使用 Laravel Echo

我有一个使用 Laravel 5.4 构建的网站,并且正在使用 Laravel Echo 和 Pusher 构建广播系统,但不幸的是,文档缺少指定使其在没有 Vue.js 的情况下工作所需的步骤。

有人用过这个配置吗?由于正确安装了 Echo,我需要一个完整的分步指南(我更喜欢 CDN,但找不到)。

pusher laravel-echo laravel-5.4

3
推荐指数
1
解决办法
9046
查看次数

Laravel echo 不收听

我来自委内瑞拉,我在尝试将 Laravel echo 与项目集成时遇到问题,我尝试了一切,我想得到你的帮助。

也许我没有以正确的方式工作。

我正在尝试将广播事件与 socket io 一起使用,因为我在一家初创公司工作,并且很难支付像 pusher 这样的服务。

我正在尝试进行简单的实时聊天。

那是我的 chatController ..

public function index(Request $request){

  $sender=Auth::user()->id;
  $receiver=\App\User::find(2);
  $message=new Message;
  $message->message="Hola, esto es una prueba de eventos";
  $message->user_receiver_id=$receiver->id;
  $message->user_sender_id=Auth::user()->id;
  $message->id_chat=1;
  $message->save();

  event(new \App\Events\sendMessage($message,$receiver));

  return response()->json(["status"=>"ok"]);
}
Run Code Online (Sandbox Code Playgroud)

该聊天根据确定的组(聊天)将消息存储在数据库中。

将被触发的事件是:

类 sendMessage 实现 ShouldBroadcast { 使用 Dispatchable、InteractsWithSockets、SerializesModels;

/**
 * Create a new event instance.
 *
 * @return void
 */

public $message;
public $user_receiver;

public function __construct(Message $message,User $user_receiver)
{
    $this->message=$message;
    $this->user_receiver=$user_receiver;

}

public function broadcastOn()
{
    \Log::info($this->message->id_chat);

    return new …
Run Code Online (Sandbox Code Playgroud)

php jquery laravel laravel-echo laravel-artisan

3
推荐指数
2
解决办法
1万
查看次数

PusherBroadcaster.php 第 106 行中的 Laravel Echo BroadcastException:未知 auth_key

尝试广播事件时出现未知 auth_key 错误。我尝试像其他帖子中提到的那样更改我的集群,但没有工作。我在 app.php 中有未注释的广播服务提供者。

.env

PUSHER_APP_ID=******
PUSHER_APP_KEY=******
PUSHER_APP_SECRET=******
Run Code Online (Sandbox Code Playgroud)

配置/广播

 'pusher' => [
  'driver' => 'pusher',
  'key' => env('PUSHER_APP_KEY'),
  'secret' => env('PUSHER_APP_SECRET'),
  'app_id' => env('PUSHER_APP_ID'),
  'options' => [
   ],
],
Run Code Online (Sandbox Code Playgroud)

\app\Events\MessagePosted.php

public function broadcastOn() {
  return new PresenceChannel('chatroom');
 }
Run Code Online (Sandbox Code Playgroud)

\resources\assets\js\bootstrap.js

window.axios = require('axios');

window.axios.defaults.headers.common = {
    'X-Requested-With': 'XMLHttpRequest'
};

import Echo from "laravel-echo"
window.Pusher = require('pusher-js');
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '*****',
    cluster: 'ap2',
    encrypted: true
});
Run Code Online (Sandbox Code Playgroud)

\resources\assets\js\app.js

const app = new Vue({
    el: '#app',
    data: {
        messages: …
Run Code Online (Sandbox Code Playgroud)

php pusher laravel-echo laravel-5.4

3
推荐指数
1
解决办法
4212
查看次数

AWS EB:WebSocket握手期间出错:意外的响应代码:400

在AWS上托管了一个Laravel / Vue.JS应用,该应用在Classic Load Balancer(Elastic Beanstalk)之后,并通过Nginx内部代理到socket.io服务器。SSL在Nginx上终止。

这是nginx配置:

location /socket.io {
    proxy_pass          http://127.0.0.1:6001;
    proxy_http_version  1.1;
    proxy_set_header    Upgrade           $http_upgrade;
    proxy_set_header    Connection        "upgrade";
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP         $remote_addr;
    proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto $scheme;
}
Run Code Online (Sandbox Code Playgroud)

现在,长轮询模式可以正常运行,但是无法启动升级:

WebSocket与'wss://example.com/socket.io/?EIO = 3&transport = websocket&sid = HmDFtq-aj1WgfGUyAAAJ'的连接失败:WebSocket握手期间出错:意外的响应代码:400

PS Chrome的Frames标签中,我只能看到以下奇怪消息:(Opcode -1)

有没有人成功让socket.io在AWS Elastic Beanstalk环境中工作?我只花了两个星期来处理这个问题,非常感谢任何建议或想法。谢谢!

更新。我打开了详细的日志记录,这是Nginx中的变量:

$host example.com
$proxy_add_x_forwarded_for 134.xxx.xxx.xxx
$http_upgrade -
$remote_addr 172.31.10.208
$remote_user -
$server_name _
$upstream_addr 127.0.0.1:6001
$request GET /socket.io/?EIO=3&transport=polling&t=Lw26sYn&sid=6L5iHma-GJOeE3JQAAAX HTTP/1.1
$upstream_response_time 24.658 msec
$request_time 24.658
Run Code Online (Sandbox Code Playgroud)

也许有人会发现其中一些值不正确,所以我将不胜感激。

nginx amazon-web-services socket.io amazon-elastic-beanstalk laravel-echo

3
推荐指数
1
解决办法
3597
查看次数

Laravel Echo回调

我正在研究Laravel Echo(使用socket.io作为连接器)

但是我找不到用户/访问者成功或未连接到套接字(非通道)时如何绑定回调的方法,但通常无法连接。

import Echo from "laravel-echo"; //import Laravel-echo

if(typeof(io) != 'undefined'){ //check if io loaded
    //init Echo
    window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: { path: '/socket.io' }
    });
}
Run Code Online (Sandbox Code Playgroud)

所以在这里我检查io是否存在,然后很可能套接字已启动。

但是我们可以像使用socket.io一样绑定回调吗:来自socket.io文档的示例

const socket = io('http://localhost');

console.log(socket.id); // undefined

socket.on('connect', () => {
  console.log(socket.id); // 'here we can get socket id'
});
Run Code Online (Sandbox Code Playgroud)

我需要回调的原因是获取套接字ID并启动其他脚本。

php socket.io laravel-5 laravel-echo laravel-5.5

3
推荐指数
1
解决办法
2275
查看次数

Laravel 广播通知 - 尝试发送通知时出现“array_key_exists()”错误

我正在尝试在数据更改时向用户发送广播通知。但是每次我尝试发送通知时,都会收到此错误:

array_key_exists(): The first argument should be either a string or an integer.

这是我的通知:

class MyNotification extends Notification
{
    use Queueable;

    public $games;

    public $title;
    public $summary;
    public $url;

    public function __construct($games)
    {
        $this->games = $games;

        $this->title = "Your games";
        $this->summary = "Games for {$games[0]->team}";
        $this->url = "/some/url";
    }

    public function via($notifiable)
    {
        return ['mail','broadcast','database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return …
Run Code Online (Sandbox Code Playgroud)

broadcast laravel laravel-echo laravel-notification laravel-broadcast

3
推荐指数
1
解决办法
453
查看次数

Laravel Pusher:让所有用户连接到 Presence Channel

有谁知道如何从状态通道获取所有连接的用户?我的 API 控制器中需要它们,并将它们全部保存在数据库中。

我发现有些人使用Pusher::get(),但后来我收到一条错误消息Non-static method Pusher\Pusher::get() should not be called statically。我见过其他人使用$this->pusher('/channels'),但是我从哪里获取 $pusher 实例?

抱歉,也许是个菜鸟问题?

laravel pusher laravel-echo

3
推荐指数
1
解决办法
2301
查看次数

Laravel Echo + Websockets + 私人频道

我知道我不是第一个为此苦苦挣扎的人。但几天后,经过很多相关的问题,我觉得我的案子值得它自己的问题:)。

我有一个可用的 websocket 解决方案,包括 Laravel Websockets ( https://beyondco.de/docs/laravel-websockets/getting-started/introduction ) 和用于公共频道的 Laravel Echo 。我的客户端应用程序是一个 vue-cli 应用程序,连接到服务器+公共频道上的广播消息效果很好。授权由 Laravel Passport 处理。因此,通过在授权标头中发送承载令牌,后端应用程序可以知道用户是否已通过身份验证。

然而,我正在努力让私人频道正常工作。尝试进行身份验证总是给我这个错误:

Access to XMLHttpRequest at 'https://my-app.test/broadcasting/auth' from origin 'https://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

我知道当我遇到服务器问题时会出现此 CORS 错误,因此我尝试在 Insomnia 中调试请求。然而,当模仿 Insomnia 中的请求时,它会给出响应 200 以及预期的结果:

Insomnia 中的模仿身份验证请求

我一直在阅读一些指南和 stackoverflow 问题,但找不到类似的内容。回到它可能是 CORS 问题,但我认为情况并非如此。我的 OPTIONS 请求返回得很好。

为了完整起见,我还添加了一些可能有助于调试的代码。

我的广播服务提供商

public function boot()
{
    Broadcast::routes(['middleware' => ['auth:api']]);

    require base_path('routes/channels.php');
}
Run Code Online (Sandbox Code Playgroud)

我的频道.php …

laravel laravel-echo laravel-websockets

3
推荐指数
1
解决办法
4896
查看次数