/// <summary> /// Constructor of a presence watcher supporting persistent, polling and on-demand subscriptions /// to presence categories published by specified remote presentities. on-demand subscription is /// also known as presence query. The code here also handles self-presence. /// </summary> /// <param name="endpoint"></param> public PresenceWatcher(LocalEndpoint endpoint) { _remotePresenceServices = endpoint.PresenceServices; // RemotePresenceView for persitent subscription: RemotePresenceViewSettings rpvs = new RemotePresenceViewSettings(); rpvs.SubscriptionMode = RemotePresenceViewSubscriptionMode.Persistent; _persistentPresenceView = new RemotePresenceView(endpoint, rpvs); _persistentPresenceView.PresenceNotificationReceived += new EventHandler<RemotePresentitiesNotificationEventArgs>(PersistentPresenceReceivedEventHandler); _persistentPresenceView.SubscriptionStateChanged += new EventHandler<RemoteSubscriptionStateChangedEventArgs>(PersistentSubscriptionStateChangedEventHandler); // RemotePresenceView for polling subscription rpvs = new RemotePresenceViewSettings(); rpvs.SubscriptionMode = RemotePresenceViewSubscriptionMode.Polling; rpvs.PollingInterval = new TimeSpan(0, 5, 0); // every 5 minutes _pollingPresenceView = new RemotePresenceView(endpoint, rpvs); _pollingPresenceView.SetPresenceSubscriptionCategoriesForPolling(new string[] { "contactCard", "state", "note", "noteHistory" }); _pollingPresenceView.PresenceNotificationReceived += new EventHandler<RemotePresentitiesNotificationEventArgs>(PollingPresenceReceivedEventHandler); _pollingPresenceView.SubscriptionStateChanged += new EventHandler<RemoteSubscriptionStateChangedEventArgs>(PollingSubscriptionStateChangedEventHandler); }
// Helper method that is called by the imFlow_MessageReceived delegate. // This method returns the URI of the first available contact in a particular group. // The group is determined in the handler for the NotificationReceived event, // the ContactGroupServices_NotificationReceived delegate. private Uri GetFirstAvailableContact() { int ThreadID = Thread.CurrentThread.ManagedThreadId; Console.WriteLine("In getFirstAvailable - thread ID: " + ThreadID); RemotePresenceViewSettings presenceViewSettings = new RemotePresenceViewSettings(); presenceViewSettings.SubscriptionMode = RemotePresenceViewSubscriptionMode.Persistent; _remotePresenceView = new RemotePresenceView(_userEndpoint, presenceViewSettings); _remotePresenceView.PresenceNotificationReceived += new EventHandler < RemotePresentitiesNotificationEventArgs>(RemotePresenceView_PresenceNotificationReceived); _userEndpoint.ContactGroupServices.NotificationReceived += new EventHandler <Microsoft.Rtc.Collaboration.ContactsGroups.ContactGroupNotificationEventArgs>( ContactGroupServices_NotificationReceived); Console.WriteLine("In getFirstAvailable, ContactGroupServices state: {0}", _userEndpoint.ContactGroupServices.CurrentState.ToString()); _userEndpoint.ContactGroupServices.BeginSubscribe(ContactGroupSubscribeCB, _userEndpoint.ContactGroupServices); return(_remoteContactUri); }
private void StartupPresenceView(AsyncTask task, object state) { task.DoFinalStep( delegate() { CallbackRequest callbackRequest = (CallbackRequest)state; var viewSettings = new RemotePresenceViewSettings(); viewSettings.SubscriptionMode = RemotePresenceViewSubscriptionMode.Persistent; var presenceView = new RemotePresenceView(callbackRequest.CutomerEndpoint, viewSettings); presenceView.ApplicationContext = callbackRequest; var target = new RemotePresentitySubscriptionTarget(callbackRequest.TargetUri); presenceView.PresenceNotificationReceived += this.PresenceView_NotificationReceived; presenceView.SubscriptionStateChanged += this.PresenceView_SubscriptionStateChanged; callbackRequest.PresenceView = presenceView; presenceView.StartSubscribingToPresentities( new RemotePresentitySubscriptionTarget[] { target }); }); }
/// <summary> /// Retrieves the application configuration and begins running the /// sample. /// </summary> private void Run() { // Prepare and instantiate the platform and an endpoint. _helper = new UCMASampleHelper(); _userEndpoint = _helper.CreateEstablishedUserEndpoint( "SubscribePresenceView Sample User" /*endpointFriendlyName*/); // Get the Uri of the remote user to subscribe to. _remoteUserUri = "sip:" + UCMASampleHelper.PromptUser( "Please enter the URI, in the format User@Host, of the user to subscribe to => ", "RemoteUserURI"); Console.WriteLine("\nChanging PresenceSubscriptionCategory Filter to only include ContactCard " + "and PresenceState"); // Set category filter. This is a global filter for all persistent // subscriptions and can only be changed before any subscriptions // are active. // BUGBUG: error CS0618: 'Microsoft.Rtc.Collaboration.LocalEndpoint.RemotePresence' is obsolete: 'This property will be removed from future Versions. Please see RemotePresenceView and PresenceServices instead.' // _userEndpoint.RemotePresence.PresenceSubscriptionCategories = // new string[] { PresenceCategoryNames.ContactCard, PresenceCategoryNames.State }; // RemotePresencView objects can be used to group subscriptions. // When a RemotePresenceView is created, it is created with the // specified RemotePresenceViewSettings and associated with the // specified LocalEndpoint. The views can then be accessed via the // LocalEndpoint setting: RemotePresenceViews. // RemotePresenceView.ApplicationContext can be used to pass or // store information related to the view (seen below). // Create a RemotePresenceView with a persistent subscription mode. // This type of view can represent a contact list, for example. // Note: The Default SubscriptionMode will start a subscription as // Persistent and downgrade to Polling if an error occurs. var persistentSettings = new RemotePresenceViewSettings(); persistentSettings.SubscriptionMode = RemotePresenceViewSubscriptionMode.Default; _persistentView = new RemotePresenceView(_userEndpoint, persistentSettings); _persistentView.ApplicationContext = "Persistent View"; // Wire up event handlers for the view this.WireUpHandlersForView(_persistentView); // Create a RemotePresenceView with a polling subscription mode // This type of view can represent a list of people // on the To: line of an e-mail, for example. var pollingSettings = new RemotePresenceViewSettings(); pollingSettings.SubscriptionMode = RemotePresenceViewSubscriptionMode.Polling; // The line below is not necessary; PollingInterval has a default // (and minimum) value of 5 minutes. pollingSettings.PollingInterval = TimeSpan.FromMinutes(5); _pollingView = new RemotePresenceView(_userEndpoint, pollingSettings); _pollingView.ApplicationContext = "Polling View"; // Wire up event handlers for the view this.WireUpHandlersForView(_pollingView); Console.WriteLine("\nChanging Polling View's category filter to only include Note."); _pollingView.SetPresenceSubscriptionCategoriesForPolling( new string[] { PresenceCategoryNames.Note }); try { // This constructor does very basic validation on the uri _target = new RemotePresentitySubscriptionTarget(_remoteUserUri); } catch (ArgumentException argumentException) { // ArgumentException will be thrown if the parameter used to // create the RemotePresentitySubscriptionTarget is an // invalid sip Uri. // TODO (Left to the reader): Error handling code to either // retry creating the target with corrected parameters, log // the error for debugging or gracefully exit the program. Console.WriteLine(argumentException.ToString()); throw; } Console.WriteLine("\nInitiating subscriptions for both Views to user: "******"succeed", but the StateChanged notifications will indicate the // subscription went to a Terminated state. _persistentView.StartSubscribingToPresentities(new RemotePresentitySubscriptionTarget[] { _target }); _pollingView.StartSubscribingToPresentities(new RemotePresentitySubscriptionTarget[] { _target }); // There is no callback for the StartSubscribingToPresentities // operation because subscriptions to multiple targets can // complete at different times. Completion or failure of the // subscription can be monitored through the // SubscriptionStateChanged event handler, // RemotePresenceView_NotificationReceived. UCMASampleHelper.PauseBeforeContinuing("Press ENTER to unsubscribe."); Console.WriteLine("\nBoth Views are terminating any subscriptions to user: "******"Press ENTER to shutdown and exit."); // Shutdown Platform _helper.ShutdownPlatform(); }