ajax - 如何广播一个可以为每个用户专门定制的 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");
}
});
}));
});
解决方案
嗯,我不能从您的代码中完全看出,但是如果您有用户 ID 或用户名之类的东西,您能否为每个 UUID 创建一个通道,然后在您需要发送“自定义”事件时;只是在那个私人频道上发送?
推荐阅读
- java - 使用收集框架删除重复值
- android - 如何在一个主菜单下调用多个 xml 文件并且我不同时使用 Fragments?
- ios - 有没有人在 iOS Flutter 上看到这个带有 stripe_payments 的编译错误?
- css - 未计算或应用复杂的 CSS 计算
- c# - WPF 窗口样式应用于错误的窗口
- performance - 如何在反应原生生产构建中访问 global.nativePerformanceNow()?或任何其他测量时间戳的方法
- java - 请求属性在从一个 Servlet 转发到另一个 Servlet 的过程中丢失
- python - 多处理池管理器命名空间 EOF 错误
- c# - .NET 默认十进制格式
- kubernetes - 将 YAML 转换为 JSON 时出错:yaml:没有找到预期的密钥