⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subscribe.cs

📁 Csharp网络应用案例导航 Csharp网络应用案例导航
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using PublishSubscribe;
using Ticker;

/// usings for remoting
/// need to add the System.Runtime.Remoting reference
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace PublishSubscribe
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class SubscribeClient : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox SubscribeTo;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ListView listViewSubscriptions;
		private System.Windows.Forms.ColumnHeader Subscriptions;
		private System.Windows.Forms.ColumnHeader Value;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.NumericUpDown InitialValue;

		/// <summary>
		/// 定义一个“发布者”实例,以便回调
		/// Maintain a publsher object so that the callbacks can be set up
		/// </summary>
		private Publisher NewPublisher;

		/// <summary>
		///定义一个“订阅者”实例
		/// Create an instance of the subscriber dll
		/// </summary>
		private static Subscriber NewSubscriber;

		/// <summary>
		/// 定义处理回调的通道
		/// Set up the tcp channel to listen for callbacks
		/// </summary>
		private TcpChannel ClientChannel;

		/// <summary>
		/// Set a timer to check if any updates have occurred
		/// </summary>
		private Timer timer;
		/// <summary>
		/// 计数器事件响应函数
		/// 该函数用于更新输出的ListView控件
		/// </summary>
		/// <param name="PassedObject"></param>
		/// <param name="TimerArgs"></param>
		private void TimerEvent( object PassedObject, EventArgs TimerArgs )
		{
			if( NewSubscriber.IsUpdated == true )
			{
				/// redraw the list box
				listViewSubscriptions.Clear();
				ListViewItem lvItem;

				ArrayList temp = NewSubscriber.GetDataList;
				SubscriberData tempSubscriber;
				///ColumnHeader对象为ListView控件的第一行(属性行)
				ColumnHeader header = new ColumnHeader();
				header.Width = 100;
				///第一列标题:
				header.Text = "Subscription";
				listViewSubscriptions.Columns.Add( header );
				ColumnHeader header2 = ( ColumnHeader )header.Clone();
				///第二列标题:
				header2.Text = "Value";
				listViewSubscriptions.Columns.Add( header2 );
				///将每个用户的订阅情况加入到ListView中
				for( int i=0; i<temp.Count; i++ )
				{
					tempSubscriber = ( SubscriberData )temp[ i ];

					lvItem = listViewSubscriptions.Items.Add( tempSubscriber.Name );
					lvItem.SubItems.Add( tempSubscriber.Value.ToString() );

				}
				
				NewSubscriber.IsUpdated = false;
			}
		}

		public SubscribeClient()
		{
			//
			// 初始化窗口控件
			//
			InitializeComponent();

			//
			// 创建一个Subscriber实例
			//

			NewSubscriber = new Subscriber();

			/// 创建一个通道监听回调
			/// 端口号为0允许系统监听所有的通道,在这里不能指定端口8090。
			/// create a channel to listen for the callbacks 
			/// 0 allows the system to decide the port and you can't listen directly to the
			/// broadcast port 8090 in this case
			ClientChannel = new TcpChannel( 0 );

			///注册客户通道
			///register the acceptance of the required channel
			ChannelServices.RegisterChannel( ClientChannel );

			///通过通道获得发布者对象
			///get the serverobject from the required channel
			try
			{
				NewPublisher = ( Publisher )Activator.GetObject( typeof( Publisher ), "tcp://localhost:8090/PublishSubscribe" ); 
			}
			///无法获得则捕捉异常,异常情况有可能是:指定的url地址不可达或者指定获得的对象定义不对
			catch( NullReferenceException nullExp )
			{
				MessageBox.Show( "The url for the object is invalid " + nullExp.Message );
			}
			catch( RemotingException remExp )
			{
				MessageBox.Show( "The object type is not defined properly, it needs to be derived for a remoting class " + remExp.Message );
			}
			///对象成功获得
			if( NewPublisher != null )
			{
			///通过Delegate对象PublishedTickerFunction
			///将NewSubsriber对象的OnSubscriberUpdate方法和NewPublisher的OnUpdatedFakeTicker事件绑定在一起
			///当NewPublisher的OnUpdatedFakeTicker事件发生时,OnSubscriberUpdate方法将被调用。
				try
				{
					NewPublisher.OnUpdatedFakeTicker += new Publisher.PublishedTickerFunction( NewSubscriber.OnSubscriberUpdate );
				}
				catch( Exception exp )
				{
					MessageBox.Show( exp.Message );
				}
			}

			///实例化一个计数器
			timer = new Timer();
			/// 设置计数器的事件响应函数
			/// 需要注意的事,该响应函数的参数必须包含一个对象和一个事件处理句柄
			/// set up the timer event handler
			/// Note the EventHandler requires a void function that takes a object and
			/// an event handler as a parameter
			
			timer.Tick += new EventHandler( TimerEvent );
			///设定计数间隔 
			///set the interval
			timer.Interval = 3000;
			///开始计数 
			///start the timer
			timer.Start();
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.SubscribeTo = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.listViewSubscriptions = new System.Windows.Forms.ListView();
			this.Subscriptions = new System.Windows.Forms.ColumnHeader();
			this.Value = new System.Windows.Forms.ColumnHeader();
			this.button1 = new System.Windows.Forms.Button();
			this.InitialValue = new System.Windows.Forms.NumericUpDown();
			((System.ComponentModel.ISupportInitialize)(this.InitialValue)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(20, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(185, 19);
			this.label1.TabIndex = 0;
			this.label1.Text = "订阅者";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(10, 37);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(195, 19);
			this.label2.TabIndex = 1;
			this.label2.Text = "姓名";
			// 
			// SubscribeTo
			// 
			this.SubscribeTo.Location = new System.Drawing.Point(10, 65);
			this.SubscribeTo.Name = "SubscribeTo";
			this.SubscribeTo.Size = new System.Drawing.Size(195, 21);
			this.SubscribeTo.TabIndex = 2;
			this.SubscribeTo.Text = "Subcription name";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(10, 93);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(195, 26);
			this.label3.TabIndex = 3;
			this.label3.Text = "初始值";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(297, 9);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(215, 19);
			this.label4.TabIndex = 5;
			this.label4.Text = "订阅情况列表";
			// 
			// listViewSubscriptions
			// 
			this.listViewSubscriptions.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																									this.Subscriptions,
																									this.Value});
			this.listViewSubscriptions.FullRowSelect = true;
			this.listViewSubscriptions.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listViewSubscriptions.Location = new System.Drawing.Point(297, 37);
			this.listViewSubscriptions.MultiSelect = false;
			this.listViewSubscriptions.Name = "listViewSubscriptions";
			this.listViewSubscriptions.Size = new System.Drawing.Size(246, 241);
			this.listViewSubscriptions.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listViewSubscriptions.TabIndex = 6;
			this.listViewSubscriptions.View = System.Windows.Forms.View.Details;
			// 
			// Subscriptions
			// 
			this.Subscriptions.Text = "Subscriptions";
			this.Subscriptions.Width = 128;
			// 
			// Value
			// 
			this.Value.Text = "Value";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(10, 157);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 27);
			this.button1.TabIndex = 7;
			this.button1.Text = "订阅";
			this.button1.Click += new System.EventHandler(this.OnSubscribe);
			// 
			// InitialValue
			// 
			this.InitialValue.Location = new System.Drawing.Point(10, 111);
			this.InitialValue.Maximum = new System.Decimal(new int[] {
																		 32768,
																		 0,
																		 0,
																		 0});
			this.InitialValue.Name = "InitialValue";
			this.InitialValue.Size = new System.Drawing.Size(195, 21);
			this.InitialValue.TabIndex = 8;
			// 
			// SubscribeClient
			// 
			this.AutoScale = false;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(552, 293);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.InitialValue,
																		  this.button1,
																		  this.listViewSubscriptions,
																		  this.label4,
																		  this.label3,
																		  this.SubscribeTo,
																		  this.label2,
																		  this.label1});
			this.MaximizeBox = false;
			this.Name = "SubscribeClient";
			this.Text = "订阅者";
			((System.ComponentModel.ISupportInitialize)(this.InitialValue)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new SubscribeClient());
		}
/// <summary>
/// 增加一个“订阅者”
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
		private void OnSubscribe(object sender, System.EventArgs e)
		{
			if( NewPublisher != null )
			{
				///在服务端(“发布者”)上注册新的订阅者
				///订阅数目从NumbericUpDown控件中获得
				try
				{
					NewPublisher.RegisterFakeTicker( SubscribeTo.Text, ( int )InitialValue.Value );
				}
				catch( InvalidCastException invExp )
				{
					MessageBox.Show( invExp.Message );
				}
			}
			
		}

		private void OnPublish(object sender, System.EventArgs e)
		{
			NewPublisher.PublishNewEvents();
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -