ProfileUpdated.php:
class ProfileUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $data;
public function __construct()
{
$this->data = [
'power'=> '10'
];
}
public function broadcastOn()
{
return new PrivateChannel('test-channel');
}
}
bootstrap.js:
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':3000',
authEndpoint: 'http://localhost/sock/public/broadcasting/auth',
auth: {
headers: {
Authorization: 'Bearer ' + '123456',
},
},
});
channels.php:
Broadcast::channel('test-channel.{id}', function ($user, $id) {
return true;
});
views/layouts/master.blade.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FiveOne Socket.io</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
@yield('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="{{ asset('js/app.js') }}"></script>
@yield('footer')
</body>
</html>
views/layouts/test.blade.php:
@extends('layouts.master')
@section('content')
<p id="power">0</p>
@stop
@section('footer')
{{-- <script src="{{ asset('js/socket.io.js') }}"></script> --}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script>
window.Echo.private('test-channel.1')
.listen('ProfileUpdated', (message) => {
console.log(message);
$('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
});
</script>
@stop
laravel-echo-server.js:
{
"authHost": "http://localhost",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "16d858df454bc9ca",
"key": "2b2a5e635e336904671891632b15db3d"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "3000",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "*",
"allowHeaders": "*"
}
}
config/app.php:
App\Providers\BroadcastServiceProvider::class, // is commented out
web.php:
Route::get('fire', function () {
event(new App\Events\ProfileUpdated());
return "event fired";
});
Route::get('test', function () {
return view('test');
});
.env:
BROADCAST_DRIVER=redis
这些都是我所有与laravel / event / broadcast / redis / socket.io / laravel echo相关的文件。当我在控制台中运行laravel-echo-server start
时,它返回错误消息:Sending auth request to: http://localhost/broadcasting/auth
BpCgX4rFE2tf2CwRAAAA could not be authenticated to private-test-channel.1
。我该如何解决? localhost
在那里不对吗?我应该有broadcasting/auth
的路线吗?我应该有一个特定的端口吗?那里的验证是什么?如果它是对用户的身份验证,那么我在routes/channels.php
中返回了true。
请注意,我尚未将laravel默认身份验证与php artisan make:auth
一起使用。
顺便说一句,它没有任何问题,并且在使用公共频道时可以正常工作。
更新
我从以下位置编辑了laravel-echo-server.js
:
"authHost": "http://localhost",
"authEndpoint": "/broadcasting/auth",
收件人:
"authHost": "http://localhost/sock/public",
"authEndpoint": "/broadcasting/auth",
并创建了一条新路线:
Route::any('/broadcasting/auth', function () {
return 'true';
});
它返回:
-G8OS2X2BnBPWLnSAAAC authenticated for: private-test-channel.1
-G8OS2X2BnBPWLnSAAAC joined channel: private-test-channel.1
当我运行laravel-echo-server start
并返回时:
Channel: private-test-channel
Event: App\Events\ProfileUpdated
我打开http://localhost/sock/public/fire
时。
但是现在的问题是,它不在这里打印接收到的值:
window.Echo.private('private-test-channel.1')
.listen('ProfileUpdated', (message) => {
console.log(message); // HERE it doesn't print the message in console!
$('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
});
更新2
我已经从github下载并安装了Laravel-Realtime-Chat
,并且再次可以在公共频道中输入内容,但是即使在实时的公共/私人聊天中也无法发送消息。所以看来我的电脑/ Laravel /防火墙有问题。我该如何克服这个问题?
谢谢