是否可以在 laravel echo 中向通道发送数据?
Is it possible emit data to channel in laravel echo?
我有两个单独的 laravel 项目并安装在其中一个项目 laravel-echo
和另一个 laravel-echo-server
.
上
我将这个项目连接在一起,并将数据从服务器传输到客户端,但我无法将数据从客户端发送到服务器。
服务器中的事件:
class TestEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $prices;
/**
* Create a new event instance.
*
* @param int[] $prices
*/
public function __construct($prices = ['a' => 11000, 'b' => 420])
{
$this->prices = $prices;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('bla-bla');
}
public function broadcastAs()
{
return 'financial.prices';
}
}
服务器中的rotue:
Route::get('/fire', function () {
$prices = [
'a' => rand(11000, 120000),
'b' => rand(450, 5000)
];
event(new \App\Events\TestEvent($prices));
return 'done';
});
客户:
import Echo from 'laravel-echo';
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
logToConsole: true
});
window.Echo.logToConsole = true;
window.Echo.join('bla-bla')
.here((data) => {
console.log('asdasd');
})
.joining((data) => {
console.log('asdasd');
})
.leaving((data) => {
console.log('asdasd');
});
我该怎么做?如果有人能给我建议,我将不胜感激!
默认情况下,Echo 将使用 /broadcasting/auth
端点来授权频道访问。如果您的客户端不在同一主机上,您将必须自定义推送器的 authEndpoint。您可以通过将 authEndpoint
配置选项传递给您的 Echo 实例来指定您自己的授权端点:
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your-pusher-channels-key',
authEndpoint: '/custom/endpoint/auth', //customize here.
});
连接到socket服务器后需要使用emit:
window.io = require('socket.io-client');
const socket = io('https://localhost:8080');
socket.on('connect', () => {
socket.emit('your_channel_to_emit', {your_data});
});
将此添加到您的活动中(App\EventName):
public function broadcastWith()
{
return [
'data' => "your data",
'moredata' => "more data",
];
}
并在 JS 中像这样访问您的数据:
Echo.channel('channel-name')
.listen('EventName', (event) => {
console.log(event.data);
console.log(event.moredata);
console.log(event['moredata']);
}
我有两个单独的 laravel 项目并安装在其中一个项目 laravel-echo
和另一个 laravel-echo-server
.
我将这个项目连接在一起,并将数据从服务器传输到客户端,但我无法将数据从客户端发送到服务器。
服务器中的事件:
class TestEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $prices;
/**
* Create a new event instance.
*
* @param int[] $prices
*/
public function __construct($prices = ['a' => 11000, 'b' => 420])
{
$this->prices = $prices;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel('bla-bla');
}
public function broadcastAs()
{
return 'financial.prices';
}
}
服务器中的rotue:
Route::get('/fire', function () {
$prices = [
'a' => rand(11000, 120000),
'b' => rand(450, 5000)
];
event(new \App\Events\TestEvent($prices));
return 'done';
});
客户:
import Echo from 'laravel-echo';
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
logToConsole: true
});
window.Echo.logToConsole = true;
window.Echo.join('bla-bla')
.here((data) => {
console.log('asdasd');
})
.joining((data) => {
console.log('asdasd');
})
.leaving((data) => {
console.log('asdasd');
});
我该怎么做?如果有人能给我建议,我将不胜感激!
默认情况下,Echo 将使用 /broadcasting/auth
端点来授权频道访问。如果您的客户端不在同一主机上,您将必须自定义推送器的 authEndpoint。您可以通过将 authEndpoint
配置选项传递给您的 Echo 实例来指定您自己的授权端点:
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your-pusher-channels-key',
authEndpoint: '/custom/endpoint/auth', //customize here.
});
连接到socket服务器后需要使用emit:
window.io = require('socket.io-client');
const socket = io('https://localhost:8080');
socket.on('connect', () => {
socket.emit('your_channel_to_emit', {your_data});
});
将此添加到您的活动中(App\EventName):
public function broadcastWith()
{
return [
'data' => "your data",
'moredata' => "more data",
];
}
并在 JS 中像这样访问您的数据:
Echo.channel('channel-name')
.listen('EventName', (event) => {
console.log(event.data);
console.log(event.moredata);
console.log(event['moredata']);
}