📄 subscriber.cs
字号:
using System;
using System.Collections;
using Ticker;
namespace PublishSubscribe
{
/// <summary>
/// Subscriber data needs to be a public class so that it can be accessed
/// by the SubscribeClient class
/// </summary>
public class SubscriberData
{
private string strName;
/// <summary>
/// 名字属性
/// </summary>
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}
private int nValue;
/// <summary>
/// 值属性(从发布者那里得到的值,即订阅的信息)
/// </summary>
public int Value
{
get
{
return nValue;
}
set
{
nValue = value;
}
}
public SubscriberData()
{
Name = "";
Value = 0;
}
}
/// <summary>
/// subscriber 的回调函数被放在了一个dll中,以便“发布者”可以引用subscriber对象
/// The subscriber callback function is placed in a dll so that the publisher can
/// reference the subscriber and accept the class for callbacks.
/// </summary>
public class Subscriber : MarshalByRefObject
{
/// <summary>
///声明一个订阅者列表,该列表将包含所有订阅者
///的最新消息(名字和他从发布者中得到的值)
/// Declare an arraylist to hold the updated information
/// </summary>
private ArrayList DataList;
/// <summary>
///GetDataList属性
/// Get the data list and put its data in the list box
/// </summary>
public ArrayList GetDataList
{
get
{
return DataList;
}
}
/// <summary>
/// Set a boolean to true if there has been an update
/// </summary>
private bool bUpdated;
/// <summary>
/// IsUpdated属性
/// get and set for bUpdated
/// </summary>
public bool IsUpdated
{
get
{
return bUpdated;
}
set
{
bUpdated = value;
}
}
public Subscriber()
{
//
// TODO: Add constructor logic here
//
DataList = new ArrayList();
}
/// <summary>
/// 当创建于服务端的NewPublisher对象的OnUpdatedFakeTicker事件发生时,
/// 该方法将被调用。
/// Callback function called when the publisher update is triggered
/// </summary>
public void OnSubscriberUpdate( object SourceOfEvent, FakeTicker fakeTickerArg )
{
bool bFound = false;
SubscriberData temp;
///更新指定订阅者的订阅信息(从发布者那里得到的值)
for( int i=0; i<DataList.Count; i++ )
{
temp = ( SubscriberData )DataList[ i ];
if( temp.Name == fakeTickerArg.Name )
{
temp.Value = fakeTickerArg.Value;
bFound = true;
}
}
//如果订阅者列表中没有该订阅者的信息,则创建该订阅者信息,并加入列表中
if( bFound == false )
{
SubscriberData data = new SubscriberData();
data.Name = fakeTickerArg.Name;
data.Value = fakeTickerArg.Value;
DataList.Add( data );
}
IsUpdated = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -