我的 Laravel 应用程序中有一个事件,对于特定记录,它超过了 Pusher 允许的最大限制(10240 字节)。Laravel 序列化 Event 类上的每个公共属性是否正确?如果是这样,我怀疑序列化模型不应超过 10kb 限制,但无论如何它都会失败。是否有任何方法可以减少数据内容的大小?
class PostChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $post;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('post-channel.'.$this->post->id);
}
public function broadcastWith()
{
$extra = [
'data' => $this->post->data,
];
return array_merge($this->post->toArray(), $extra); …Run Code Online (Sandbox Code Playgroud) 我有一个数组,我想用随机对象填充它,但每个对象的特定百分比.例如,我有矩形,圆形和圆柱形.我希望Rectangle是数组长度的40%,Circle和Cylinder各占30%.有任何想法吗?
这段代码有40%的可能性来生成Rectangle等.
public static void main(String[] args){
n = UserInput.getInteger();
Shape[] array = new Shape[n];
for (int i=0;i<array.length;i++){
double rnd = Math.random();
if (rnd<=0.4) {
array[i] = new Rectangle();
}
else if (rnd>0.4 && rnd<=0.7){
array[i] = new Circle();
}
else {
array[i] = new Cylinder();
}
Run Code Online (Sandbox Code Playgroud)