Permalink
Browse files

Events (connect, disconnect and reconnect) implementation in

c#-windows-phone
  • Loading branch information...
1 parent e965351 commit d73dab3d4b1f51c0f5a75a000035e1fdc1e4fd16 priyanka committed Jul 9, 2012
@@ -96,22 +96,11 @@ TUTORIAL: HOW TO USE
## --------------------
//Subscribe messages of type string,json array and json object
System.Diagnostics.Debug.WriteLine("Subscribed to channel " + channel);
- Pubnub.ResponseCallback respCallback = delegate(object message)
- {
- object[] messages = (object[])message;
-
- if (messages != null && messages.Count() > 0)
- {
- for (int i = 0; i < messages.Count(); i++)
- {
- System.Diagnostics.Debug.WriteLine(messages[i]);
- }
- }
- };
+
//Subscribe messages
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("channel", channel);
- args.Add("callback", respCallback);
+ args.Add("callback", new Receiver());
pubnub.Subscribe(args);
## --------------------------------------------------------------------------------
@@ -155,4 +144,81 @@ TUTORIAL: HOW TO USE
// Get UUID
System.Diagnostics.Debug.WriteLine("UUID - > " + pubnub.UUID());
+
+## ------------------------------------------------------------------------------------
+## C#-WP7 : (Unsubscribe)
+## ----------------------
+ //Subscribe messages of type string,json array and json object
+
+ System.Diagnostics.Debug.WriteLine("Subscribed to channel " + channel);
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ args.Add("channel", channel);
+ args.Add("callback", new Receiver());
+ pubnub.Subscribe(args);
+
+ // Publish string message
+ Dictionary<string, object> strArgs = new Dictionary<string, object>();
+ string message = "Hello Windows Phone 7";
+ strArgs.Add("channel", channel);
+ strArgs.Add("message", message);
+ strArgs.Add("callback", respCallback);
+ pubnub.Publish(strArgs);
+
+ Dictionary<string, object> arg = new Dictionary<string, object>();
+ arg.Add("channel", channel);
+ //Unsubscribe messages
+ pubnub.Unsubscribe(arg);
+
+ ## ------------------------------------------------------------------------------------
+
+ //Receiver for callback
+ public class Receiver : Callback
+ {
+ public bool responseCallback(string channel, object message)
+ {
+ object[] messages = (object[])message;
+
+ if (messages != null && messages.Length > 0)
+ {
+ for (int i = 0; i < messages.Length; i++)
+ {
+ Debug.WriteLine("[Subscribed data] " + messages[i]);
+ }
+ }
+ return true;
+ }
+
+ public void errorCallback(string channel, object message)
+ {
+ Debug.WriteLine("Channel:" + channel + "-" + message.ToString());
+ }
+
+ public void connectCallback(string channel)
+ {
+ Debug.WriteLine("Connected to channel :" + channel);
+ }
+
+ public void reconnectCallback(string channel)
+ {
+ Debug.WriteLine("Reconnecting to channel :" + channel);
+ }
+
+ public void disconnectCallback(string channel)
+ {
+ Debug.WriteLine("Disconnected to channel :" + channel);
+ }
+ }
+
+ public interface Callback
+ {
+ bool responseCallback(string channel, object message);
+
+ void errorCallback(String channel, Object message);
+
+ void connectCallback(String channel);
+
+ void reconnectCallback(String channel);
+
+ void disconnectCallback(String channel);
+ }
=====================================================================================
@@ -83,22 +83,11 @@ private void btnPublish_Click(object sender, RoutedEventArgs e)
private void btnSubscribe_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Subscribed to channel " + channel);
- Pubnub.ResponseCallback respCallback = delegate(object message)
- {
- object[] messages = (object[])message;
-
- if (messages != null && messages.Count() > 0)
- {
- for (int i = 0; i < messages.Count(); i++)
- {
- System.Diagnostics.Debug.WriteLine(messages[i]);
- }
- }
- };
+
//Subscribe messages
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("channel", channel);
- args.Add("callback", respCallback);
+ args.Add("callback", new Receiver());
pubnub.Subscribe(args);
}
@@ -35,22 +35,10 @@ public SubscribeExample()
private void btnSubscribe_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Subscribed to channel " + channel);
- Pubnub.ResponseCallback respCallback = delegate(object message)
- {
- object[] messages = (object[])message;
-
- if (messages != null && messages.Count() > 0)
- {
- for (int i = 0; i < messages.Count(); i++)
- {
- System.Diagnostics.Debug.WriteLine(messages[i]);
- }
- }
-
- };
+
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("channel", channel);
- args.Add("callback", respCallback);
+ args.Add("callback", new Receiver());
pubnub.Subscribe(args);
}
}
@@ -0,0 +1,53 @@
+<phone:PhoneApplicationPage
+ x:Class="CSharp_WP7.UnsubscribeExample"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
+ xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ FontFamily="{StaticResource PhoneFontFamilyNormal}"
+ FontSize="{StaticResource PhoneFontSizeNormal}"
+ Foreground="{StaticResource PhoneForegroundBrush}"
+ SupportedOrientations="Portrait" Orientation="Portrait"
+ mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
+ shell:SystemTray.IsVisible="True">
+
+ <!--LayoutRoot is the root grid where all page content is placed-->
+ <Grid x:Name="LayoutRoot" Background="Transparent">
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="*"/>
+ </Grid.RowDefinitions>
+
+ <!--TitlePanel contains the name of the application and page title-->
+ <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
+ <TextBlock x:Name="PageTitle" Text="Example to publish and receive messages" Margin="9,7,0,0" Height="40" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+ </StackPanel>
+
+ <!--ContentPanel - place additional content here-->
+ <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
+ <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,34,0,0" Name="textBlock1" Text="Publish messages :" VerticalAlignment="Top" />
+ <Button Content="Publish" Height="70" HorizontalAlignment="Left" Margin="26,70,0,0" Name="btnPublish" VerticalAlignment="Top" Width="160" Click="btnPublish_Click" />
+ <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,146,0,0" Name="textBlock2" Text="Receive messages :" VerticalAlignment="Top" />
+ <Button Content="Subscribe" Height="70" HorizontalAlignment="Left" Margin="26,180,0,0" Name="btnSubscribe" VerticalAlignment="Top" Width="160" Click="btnSubscribe_Click" />
+ <Button Content="Unsubscribe" Height="70" HorizontalAlignment="Left" Margin="26,260,0,0" Name="btnUnsubscribe" VerticalAlignment="Top" Width="190" Click="btnUnsubscribe_Click" />
+ <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,146,0,0" Name="lblDisconnect" VerticalAlignment="Top" />
+
+
+ </Grid>
+ </Grid>
+
+ <!--Sample code showing usage of ApplicationBar-->
+ <!--<phone:PhoneApplicationPage.ApplicationBar>
+ <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
+ <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
+ <shell:ApplicationBar.MenuItems>
+ <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
+ <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
+ </shell:ApplicationBar.MenuItems>
+ </shell:ApplicationBar>
+ </phone:PhoneApplicationPage.ApplicationBar>-->
+
+</phone:PhoneApplicationPage>
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using Microsoft.Phone.Controls;
+
+namespace CSharp_WP7
+{
+ public partial class UnsubscribeExample : PhoneApplicationPage
+ {
+ //Channel name
+ string channel = "hello_world";
+
+ // Initialize pubnub state
+ Pubnub pubnub = new Pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ public UnsubscribeExample()
+ {
+ InitializeComponent();
+ }
+
+ private void btnUnsubscribe_Click(object sender, RoutedEventArgs e)
+ {
+ Dictionary<string, object> arg = new Dictionary<string, object>();
+ arg.Add("channel", channel);
+ //Unsubscribe messages
+ pubnub.Unsubscribe(arg);
+ }
+
+ private void btnSubscribe_Click(object sender, RoutedEventArgs e)
+ {
+ System.Diagnostics.Debug.WriteLine("Subscribed to channel " + channel);
+
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ args.Add("channel", channel);
+ args.Add("callback", new Receiver());
+ pubnub.Subscribe(args);
+ }
+
+ private void btnPublish_Click(object sender, RoutedEventArgs e)
+ {
+ Pubnub.ResponseCallback respCallback = delegate(object response)
+ {
+ List<object> result = (List<object>)response;
+
+ if (result != null && result.Count() > 0)
+ {
+ System.Diagnostics.Debug.WriteLine("[" + result[0].ToString() + "," + result[1].ToString() + "," + result[2].ToString() + "]");
+ }
+ };
+
+ // Publish string message
+ Dictionary<string, object> strArgs = new Dictionary<string, object>();
+ string message = "Hello Windows Phone 7";
+ strArgs.Add("channel", channel);
+ strArgs.Add("message", message);
+ strArgs.Add("callback", respCallback);
+ pubnub.Publish(strArgs);
+ }
+ }
+}
@@ -0,0 +1,26 @@
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+
+namespace CSharp_WP7
+{
+ public interface Callback
+ {
+ bool responseCallback(string channel, object message);
+
+ void errorCallback(String channel, Object message);
+
+ void connectCallback(String channel);
+
+ void reconnectCallback(String channel);
+
+ void disconnectCallback(String channel);
+ }
+}
Oops, something went wrong.

0 comments on commit d73dab3

Please sign in to comment.