我有两个单独的 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';
}
}
服务器中的路线:
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');
});
我怎样才能做到这一点?如果有人能给我建议,我将不胜感激!😊
梵蒂冈之花
慕的地6264312
开心每一天1111