首页 > 解决方案 > 如何广播一个可以为每个用户专门定制的 laravel 事件

问题描述

我在每个用户屏幕上都有一个随机产品网格供出售。当另一个用户购买产品时,如果该产品在其他用户屏幕上可见,我需要它淡出并加载新产品。鉴于产品网格是随机排序的,我怎样才能向每个用户广播他们独有的新产品(即不会复制已经在他们的屏幕上出售的产品)。我还在使用 Laravel 5.2。以前的开发人员没有升级他们的版本,我现在也无法更改。

这是当用户购买产品并在他们的屏幕上加载新产品时的 ajax 函数。

 $(document).ready(function(){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $('form.buy-product-form').on('submit', (function (e) {
                e.preventDefault();
                var product_id = $(this).closest('.product').attr("id"); //new
                var element = $(this).closest('.product');
                $.ajax({
                    url: $(this).attr('action'),
                    type: 'POST',
                    data: {'id': product_id, 'all_ids': getProductsShown()},
                    dataType: 'json',
                    success: function (data) {
                        if(data.valid_funds) {
                            $(element).css('background-color', 'red');
                            $(element).fadeOut("1000", function () {
                                $(element).fadeIn("1000", function () {
                                    $(element).animate({backgroundColor: 'green', color: 'white'}, 1000);
                                    $(element).attr("id", data.id);
                                    $(element).find("#price").html(data.price);
                                    $(element).find("#quantity").html(data.quantity);
                                    $(element).find("#seller").html(data.seller);
                                    $(element).find("#total-price").html(data.price * data.quantity);
                                    $(element).find('form').attr("action", "/UoE/buy-product/" + data.id);
                                    $(element).css('background-color', 'white');
                                });
                            });

                            updatePlayerCapital(data.player_capital, data.total_price);
                            updatePlayerUtility(data.player_utility, data.marginal_utility);
                            updatePlayerPurchases(data.purchases);

                        }else if(data.void) {
                            $(element).css('background-color', 'red');
                            $(element).fadeOut("1000", function(){
                                $(element.remove());
                            });

                            updatePlayerCapital(data.player_capital, data.total_price);
                            updatePlayerUtility(data.player_utility, data.marginal_utility);
                            updatePlayerPurchases(data.purchases);

                        }else {
                            window.alert("You do not have enough funds!");
                        }
                    },
                    error: function () {
                            window.alert("Error, product may no longer exist");
                    }
                });
            }));
        });

标签: ajaxlaraveleventslistenerbroadcasting

解决方案


嗯,我不能从您的代码中完全看出,但是如果您有用户 ID 或用户名之类的东西,您能否为每个 UUID 创建一个通道,然后在您需要发送“自定义”事件时;只是在那个私人频道上发送?


推荐阅读