php - Laravel5.8 推送器不工作。收不到
问题描述
我正在创建实时聊天应用程序。我已经在我的 laravel 和 vue.js 项目中设置了 pusher。
但它不起作用。虽然我在控制台中没有任何错误。另外,我在网络选项卡中没有错误。
我需要创建信使应用程序,所以我需要一个实时聊天功能。现在,我可以推送用户的评论,但在其他用户的窗口中,什么也没有显示。
但它确实如此,一旦我刷新页面。我认为我的推送器设置有问题,因为在推送器调试控制台中,没有执行任何会话。
这是我的代码。.env
BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
PUSHER_APP_ID=my id
PUSHER_APP_KEY=my app key
PUSHER_APP_SECRET= my secret key
PUSHER_APP_CLUSTER=mt1
广播.php
'pusher' => [
'driver' => 'pusher',
'key' => env('my key'),
'secret' => env('my secret key'),
'app_id' => env('my id'),
'options' => [
'cluster' => 'ap3',
'encrypted' => true,
],
广播服务提供者.php
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
引导程序.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'my key',
cluster: 'ap3',
encrypted: true
});
新消息.php
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->to);
}
public function broadcastWith()
{
$this->message->load('fromContact');
return ["message" => $this->message];
}
}
路线/通道.php
use Illuminate\Support\Facades\Broadcast;
联系人控制器.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Message;
class ContactsController extends Controller
{
public function get() {
$contacts = User::where('id', '!=', auth()->id())->get();//全部のcontactをjson経由でgetする
return response()->json($contacts);
}
public function getMessagesFor($id) {
$messages = Message::where('from', $id)->orWhere('to', $id)->get();
return response() -> json($messages);
}
public function send(Request $request) {
$message = Message::create([
'from' => auth()->id(),
'to' => $request->contact_id,
'text' => $request->text
]);
return response()->json($message);
}
}
这是我尝试过的。
根据 laravel 文档运行所有命令。
php artisan chache:clear 并再次运行服务器。
运行php artisan queue:work 在命令终端
解决方案
你写了吗
window.Echo.private('channelName').listen('EventName',function(e){
})
在您的应用程序中收听
推荐阅读
- python - 如何使用请求从该页面获取数据?
- ios - 使用未解析的标识符“AVAsset”
- node.js - 打字稿:使用 ts-node-dev 导入表达意外的标识符
- c# - 如何使用 SSH.NET 限制上传速度?
- android - Renderscript - 为什么参数的类型是'in' a uchar4?
- python-3.x - 查找列值是否包含在python的列表中
- javascript - 如何从ajax的databtable按钮传递学生ID以打开另一个页面并通过邮寄方式获取
- powershell - 框架不同部分的自动化
- mysql - 限制用户的服务器资源时出错
- mongodb - MongoDB Compass 使用 ssl 立即断开连接,但从命令行可以正常工作