信号对象不保持状态
我在 blazor 上有一个 asp 托管的 webasenbly,使用 SignalR 将数据从服务器发送到客户端。这是服务器向客户端发送卡片:
FounderCard cardToSend = LinkedInCards.GetOne();
int ReaminigCards = LinkedInCards.Count;
cardToSend.IsLeaderOfBand= true;
cardToSend.IsSelected = true;
await Hub.Clients.All.SendAsync(MessageOrders.RevealLinkdinCard.ToString(), cardToSend, ReaminigCards);
这是接收客户端:
hub.On<FounderCard, int>(MessageOrders.RevealLinkdinCard.ToString(), (card, remainig) => {
Game.RevealLinkedinCard(card, remainig);
});
PS:该方法适用于其他复杂结构,如玩家数据,所有字段都以正确的格式到达目的地...
以防万一有帮助,这是发送的 FounderCard 的定义:
public class FounderCard : Card
{
//base clase Card just implements Serialize and INotifyPropertyChanged
public Sections Section;
public Races Race;
public string Color => FounderCommons.FromSectionToColor(this.Section);
public string ID = String.Empty;
public bool IsLeaderOfBand;
public bool IsSelected;
public void SetID(int counter)
{
this.ID = FounderCommons.GetSectionID(this.Section) + "_" + FounderCommons.GetRaceID(this.Race) + "_" + counter.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
System.Text.Json(SignalR 默认使用的 Json 库)默认不序列化字段。您需要通过添加
{ get; 将字段更改为属性放; }
,或者您可以在字段上设置属性[JsonInclude]
。更多信息请访问 https://learn.microsoft.com/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0#include-fieldsSystem.Text.Json (the Json library being used by default in SignalR), doesn't serialize fields by default. You either need to change the fields to properties by adding
{ get; set; }
, or you can set the attribute[JsonInclude]
on the fields. More info can be found at https://learn.microsoft.com/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0#include-fields