信号对象不保持状态

发布于 2025-01-21 00:12:10 字数 1576 浏览 0 评论 0 原文

我在 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);
            });

但是对象是新的。枚举上的值是 0 值。 输入图片此处描述

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();
        }
    }

I have an asp-hosted webasenbly on blazor using SignalR to send data from the server to the client. Here's the server sending a card to the client:

FounderCard cardToSend = LinkedInCards.GetOne();
int ReaminigCards = LinkedInCards.Count;

cardToSend.IsLeaderOfBand= true;
cardToSend.IsSelected = true;

await Hub.Clients.All.SendAsync(MessageOrders.RevealLinkdinCard.ToString(), cardToSend, ReaminigCards);

here's the debug when sending the object, clearly properly filled in:
enter image description here

Here's the receiving client:

hub.On<FounderCard, int>(MessageOrders.RevealLinkdinCard.ToString(), (card, remainig) => {
                Game.RevealLinkedinCard(card, remainig);
            });

But the object is anew. The values on the enums are the 0 value..
enter image description here

PS: the method work for other complex structures as player data, all fields arrive properly formatted to the destination ...

Just in case it helps, this is the definition of the FounderCard being sent:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2025-01-28 00:12:10

System.Text.Json(SignalR 默认使用的 Json 库)默认不序列化字段。您需要通过添加 { get; 将字段更改为属性放; },或者您可以在字段上设置属性[JsonInclude]。更多信息请访问 https://learn.microsoft.com/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0#include-fields

System.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文