-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service message types for presence API (#2121)
* Add service message type for presence API
- Loading branch information
Showing
8 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Microsoft.Azure.SignalR.Protocols/Models/GroupMember.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using MessagePack; | ||
using static Microsoft.Azure.SignalR.Protocol.MessagePackUtils; | ||
|
||
namespace Microsoft.Azure.SignalR.Protocol; | ||
|
||
/// <summary> | ||
/// Represents a connection in a group. | ||
/// </summary> | ||
public record GroupMember : IMessagePackSerializable | ||
{ | ||
public string ConnectionId { get; set; } = string.Empty; | ||
|
||
public string? UserId { get; set; } | ||
|
||
void IMessagePackSerializable.Serialize(ref MessagePackWriter writer) | ||
{ | ||
writer.WriteArrayHeader(2); | ||
writer.Write(ConnectionId); | ||
writer.Write(UserId); | ||
} | ||
|
||
void IMessagePackSerializable.Load(ref MessagePackReader reader, string fieldName) | ||
{ | ||
_ = reader.ReadArrayHeader(); | ||
ConnectionId = ReadStringNotNull(ref reader, nameof(ConnectionId)); | ||
UserId = reader.ReadString(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Microsoft.Azure.SignalR.Protocols/Models/GroupMemberQueryResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using MessagePack; | ||
|
||
namespace Microsoft.Azure.SignalR.Protocol; | ||
|
||
public sealed class GroupMemberQueryResponse : IMessagePackSerializable | ||
{ | ||
/// <summary> | ||
/// The group members. | ||
/// </summary> | ||
public IReadOnlyCollection<GroupMember> Members { get; set; } = []; | ||
|
||
/// <summary> | ||
/// A token that allows the client to retrieve the next page of results. | ||
/// This parameter is provided by the service in the response of a previous request when there are additional results to be fetched. | ||
/// Clients should include the continuationToken in the next request to receive the subsequent page of data. If this parameter is omitted, the server will return the first page of results. | ||
/// </summary> | ||
public string? ContinuationToken { get; set; } | ||
|
||
void IMessagePackSerializable.Serialize(ref MessagePackWriter writer) | ||
{ | ||
writer.WriteArrayHeader(2); | ||
|
||
writer.WriteArrayHeader(Members.Count); | ||
foreach (var member in Members) | ||
{ | ||
(member as IMessagePackSerializable).Serialize(ref writer); | ||
} | ||
writer.Write(ContinuationToken); | ||
} | ||
|
||
void IMessagePackSerializable.Load(ref MessagePackReader reader, string fieldName) | ||
{ | ||
_ = reader.ReadArrayHeader(); | ||
var memberCount = reader.ReadArrayHeader(); | ||
var members = new List<GroupMember>(memberCount); | ||
for (var i = 0; i < memberCount; i++) | ||
{ | ||
members.Add(reader.Deserialize<GroupMember>("groupMembers")); | ||
} | ||
Members = members; | ||
ContinuationToken = reader.ReadString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/Microsoft.Azure.SignalR.Protocols.Tests/Models/GroupMemberQueryResponsePayloadTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.SignalR.Protocols.Tests.Models; | ||
public class GroupMemberQueryResponsePayloadTests | ||
{ | ||
[Fact] | ||
public void TestMessagePackSerialization() | ||
{ | ||
var groupMembers = new List<GroupMember> | ||
{ | ||
new GroupMember { ConnectionId = "conn1", UserId = "user1" }, | ||
new GroupMember { ConnectionId = "conn2", UserId = "user2" } | ||
}; | ||
var payload = new GroupMemberQueryResponse | ||
{ | ||
Members = groupMembers, | ||
ContinuationToken = "token" | ||
}; | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
var protocol = new ServiceProtocol(); | ||
protocol.WriteMessagePayload(payload, buffer); | ||
var deserialized = protocol.ParseMessagePayload<GroupMemberQueryResponse>(new | ||
ReadOnlySequence<byte>(buffer.WrittenMemory)); | ||
Assert.Equal(payload.ContinuationToken, deserialized.ContinuationToken); | ||
Assert.True(payload.Members.SequenceEqual(deserialized.Members)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/Microsoft.Azure.SignalR.Protocols.Tests/Models/GroupMemberTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Buffers; | ||
using Microsoft.Azure.SignalR.Protocol; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.SignalR.Protocols.Tests.Models; | ||
public class GroupMemberTests | ||
{ | ||
[Fact] | ||
public void TestMessagePackSerialization() | ||
{ | ||
var groupMember = new GroupMember() { ConnectionId = "conn", UserId = "userId" }; | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
var protocol = new ServiceProtocol(); | ||
protocol.WriteMessagePayload(groupMember, buffer); | ||
var deserialized = protocol.ParseMessagePayload<GroupMember>(new ReadOnlySequence<byte>(buffer.WrittenMemory)); | ||
Assert.Equal(groupMember, deserialized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters