我正在尝试使用socket.io。并希望将变量从我的javascript(客户端)发送到laravel事件,然后将其传递回客户端。我在代码中评论了我想要的。
下面是我的代码:
事件:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class MarketUpdate implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;
public $data;
public function __construct()
{
$this->data = [
'abcd' => $id , /// I need to get this ID from client side.
];
}
public function broadcastOn()
{
return array('channel_test');
}
}
客户端JS:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>
var id = '9';
var socket = io('http://localhost:8890');
socket.on("channel_test:App\\Events\\EventNew", function (message) {
// I Want to send id here somewhere and display
$('#abcd').html(message.data.abcd);
});
</script>
答案 0 :(得分:0)
据我了解,除了制作自处理器/处理器外,没有其他方法。我认为,您需要观看一些类似棘轮的手表。原生laravel构建没有使用消息的方法。有广播系统。
您使用ajax从客户端发送数据,然后laravel通过广播将数据发送到所有订阅的客户端