Page 31 - MSDN Magazine, May 2018
P. 31

Figure 12 Receiving Messages
private RemoteSystemSessionMessageChannel _messageChannel;
public event EventHandler<MessageReceivedEventArgs> MessageReceived = delegate { };
public void StartReceivingMessages() {
_messageChannel = new RemoteSystemSessionMessageChannel(_currentSession, "OpenChannel");
_messageChannel.ValueSetReceived += OnValueSetReceived; }
private object DeserializeMessage(ValueSet valueSet) {
Type serialType; object data;
if(valueSet.ContainsKey("user")) {
serialType = typeof(User); data = valueSet["user"];
} else if (valueSet.ContainsKey("users")) {
serialType = typeof(List<User>); data = valueSet["users"];
} else {
serialType = typeof(UserMessage);
data = valueSet["message"]; }
object value;
using (var stream = new MemoryStream((byte[])data)) {
value = new DataContractJsonSerializer(serialType).ReadObject(stream); }
return value; }
private async void OnValueSetReceived(RemoteSystemSessionMessageChannel sender, RemoteSystemSessionValueSetReceivedEventArgs args)
{
var data = DeserializeMessage(args.Message);
if (data is User) {
var user = data as User;
user.Id = args.Sender.RemoteSystem.DisplayName;
if (!Users.Contains(user)) {
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { Users.Add(user); });
}
await BroadcastMessage("users", Users.ToList()); }
else if (data is List<User>) {
var users = data as List<User>; Users.Clear();
foreach(var user in users)
{
Users.Add(user); }
} else {
MessageReceived(this, new MessageReceivedEventArgs() {
Participant = args.Sender,
Message = data });
} }
channel that’s stored in a private variable and then add a handler to the ValueSetReceived event.
Messages are sent as text and because the app is using classes as messages, I need to serialize the data. When the ValueSet is received from the channel, a DataContractJsonSerializer is used to rehydrate the message classes in the DeserializeMessage class. Because I’m unable to tell what type of message is serialized, the app will send each type of message as a different value in the ValueSet. The DeserializeMessage class will determine which key is used and return the correct class.
Once the message class is ready, the manager class will act on the message depending on its type. As you’ll see, participants will announce themselves to the host by sending their CurrentUser instance. In response, the host will broadcast the updated user
Figure 13 Broadcasting a Message
list to all participants. If the session manager receives the list of participants, it will update the Users collections with the updated data. The final option, a UserMessage, will raise a new Message- Received event that passes the message and the participant who sent the message. These additions to the RemoteSessionManager can be found in Figure 12.
Figure 12 includes a new event handler class, MessageReceived- EventArgs, that must also be created. This class contains two properties: the sender and the message:
public class MessageReceivedEventArgs {
public RemoteSystemSessionParticipant Participant { get; set; } public object Message { get; set; }
Figure 14 Sending a Direct Message
}
public async Task<bool> SendMessage(string key, object message, RemoteSystemSessionParticipant participant)
{
using (var stream = new MemoryStream()) {
new DataContractJsonSerializer(message.GetType()).WriteObject(stream, message); byte[] data = stream.ToArray();
ValueSet msg = new ValueSet();
msg.Add(key, data);
await _messageChannel.SendValueSetAsync(msg, participant);
}
return true; }
public async Task<bool> BroadcastMessage(string key, object message) {
using (var stream = new MemoryStream()) {
new DataContractJsonSerializer(message.GetType()).WriteObject(stream, message); byte[] data = stream.ToArray();
ValueSet msg = new ValueSet();
msg.Add(key, data);
await _messageChannel.BroadcastValueSetAsync(msg);
}
return true; }
msdnmagazine.com
May 2018 27


































































































   29   30   31   32   33