Page 30 - MSDN Magazine, May 2018
P. 30

Figure 9 Adding Session Discovery
Figure 10 The JoinSession Method
public MainViewModel() {
_initSessionManager = InitSessionManager(); }
private Task _initSessionManager;
private async Task InitSessionManager() {
App.SessionManager.SessionAdded += OnSessionAdded; App.SessionManager.SessionRemoved += OnSessionRemoved; await App.SessionManager.DiscoverSessions();
}
private async void OnSessionAdded(object sender, RemoteSystemSessionInfo e) {
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { Sessions.Add(e); });
}
private async void OnSessionRemoved(object sender, RemoteSystemSessionInfo e) {
if (Sessions.Contains(e)) {
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { Sessions.Remove(e); });
} }
public async Task<bool> JoinSession(RemoteSystemSessionInfo session, string name) {
bool status = true;
RemoteSystemSessionJoinResult joinResult = await session.JoinAsync();
if (joinResult.Status == RemoteSystemSessionJoinStatus.Success) {
_currentSession = joinResult.Session;
CurrentUser = new User() { DisplayName = name }; }
else {
status = false; }
InitParticipantWatcher();
return status; }
SessionAdded and SessionRemoved. Because this will be another entry point for users initializing remote sessions, you need to make sure to add a call to RemoteSystem.RequestAccessAsync. Figure 8 contains the private variable, the two events and the complete DiscoverSessions method.
It’s now possible to wire in the MainViewModel to update the Sessions property with locally available sessions. Because the DiscoverSessions method is asynchronous, the constructor of the MainViewModel needs to initialize a Task to invoke it. The initializ- ing method should also register and handle the SessionAdded and SessionRemoved events. Because these events won’t be firing on the UI thread when updating the Sessions property, it’s important to use a CoreDispatcher. The updates to MainViewModel are in Figure 9.
Connecting to the Session Once a user has identified the session they want to join and supplies a name, the RemoteSession- Manager must be able to connect them to the selected session. This will be handled by a new JoinSession method on the Remote- SessionManager, which takes the selected session and the entered display name as parameters.
The JoinSession method begins by calling the JoinAsync method on the supplied session. In return, this will fire the JoinRequested event on the host session. If the host approves the request, a success status is returned and the CurrentUser is set using the display name. As with the CreateSession method, the InitParticipantWatcher meth- od is invoked to register an event handler for when participants are added to the session. The JoinSession method is shown in Figure 10.
The final step involved in joining a session is to use the func- tionality created in the RemoteSessionManager to either create or join a session. Figure 11 shows the Start method in the MainView- Model that’s bound to the Start button in MainPage. The workflow of the method is straightforward. Depending on IsNewSession, it
calls either the CreateSession method or the JoinSession method. The results are returned by raising either the SessionConnected or ErrorConnecting events. If the session is successful, the app navigates to the MessagePage, which I’ll build in the next section.
Keep the Apps Talking
At this point, the app can successfully create or join a session and has a messaging UI that’s ready to be used. The only remaining step is enabling the devices to communicate with each other. This is accomplished by using the Remote System API to send ValueSet instances between the machines. Each ValueSet is a set of key/value pairs of serialized payloads.
Receiving Messages Messages are transmitted within a session through a RemoteSystemSessionMessageChannel. A session can have multiple channels; however, this app will need only a single channel. In the RemoteSessionManager, I’m going to add a Start- ReceivingMessages method. This method will create a new message
Figure 11 Starting a Session
public async void Start() {
if(IsNewSession) {
var result = await App.SessionManager.CreateSession(SessionName, JoinName); if(result == SessionCreationResult.Success)
{
SessionConnected(this, null); } else
{
ErrorConnecting(this, result);
}
} else
{
if(SelectedSession != null) {
var result = await App.SessionManager.JoinSession( SelectedSession as RemoteSystemSessionInfo, JoinName);
if(result) {
SessionConnected(this, null); } else
{
ErrorConnecting(this, SessionCreationResult.Failure);
} }
} }
26 msdn magazine
Universal Windows Platform


































































































   28   29   30   31   32