Yojo网页设计

yojoPHP.NETPhotoshopCSSaSPPhotoshopDreamweaverFlashFireworksCoreldrawillustratorJSPSEOSQL
·ASP基础·ASP技巧·ASP实例·Asp.Net基础·Asp.Net技巧·Asp.Net实例·PHP·JSP·编程杂谈·数据库编程

热门点击

more...
您现在的位置: yojo网页设计 >> 编程开发 >> ASP.net教程 >> Asp.Net开发技巧 >> 正文

采用 .Net Remoting 实现定向广播

更新时间:2007-11-19 23:36:33 文章来源:互联网 点击:

  相对于 WebService 来说,采用 .Net Remoting  技术的客户端能够订阅服务器端事件,这个功能简直太棒了。

  如果想利用该技术作一个简单而又典型的应用,信息广播程序是一个不错的选择。以下代码是一个简单的广播程序,当然,它实在太简陋了。

  服务端:

  Code
  class Program
  {
  static void Main(string[] args)
  {
  BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
  sfsp.TypeFilterLevel = TypeFilterLevel.Full;
  Hashtable props = new Hashtable();
  props["port"] = 8086;
  TcpChannel channel = new TcpChannel(props, null, sfsp);
  ChannelServices.RegisterChannel(channel, false);
  SayHello sayHello = new SayHello();
  RemotingServices.Marshal(sayHello, "SayHello");
  Console.ReadKey();
  sayHello.Say("Mike", "Hello, Mike");
  Console.ReadKey();
  sayHello.Say("John", "Hello, John");
  Console.ReadKey();
  }
  }


  客户端:

Code
  class Program
  {
  static void Main(string[] args)
  {
  BinaryServerFormatterSinkProvider sfsp = new BinaryServerFormatterSinkProvider();
  sfsp.TypeFilterLevel = TypeFilterLevel.Full;
  Hashtable props = new Hashtable();
  props["port"] = 0;
  TcpChannel channel = new TcpChannel(props, null, sfsp);
  ChannelServices.RegisterChannel(channel, false);
  SayHello sh = (SayHello)Activator.GetObject(typeof(SayHello), "tcp://localhost:8086/SayHello");


  SayEventReappear re = new SayEventReappear();
  re.ClientId = "John";
  sh.OnSay += new SayHandler(re.Say);
  re.OnSay += new SayHandler(re_OnSay);
  Console.ReadKey();
  }
  static void re_OnSay(string text)
  {
  Console.WriteLine(text);
  }
  }


  远程对象、委托及事件重现器(需同时部署在服务端及客户端):

Code
  public class SayHello : MarshalByRefObject
  {
  public event SayHandler OnSay;
  public void Say(string clientId, string text)
  {
  if (this.OnSay != null) this.OnSay(text);
  }
  }
  public delegate void SayHandler(string text);
  public class SayEventReappear : MarshalByRefObject
  {
  public event SayHandler OnSay;
  public void Say(string text)
  {
  if (this.OnSay != null) this.OnSay(text);

  }
  }


  OK,我的信息广播程序就这样完成了。

  但是,我很快就发现了问题:如果我的确想让所有订阅我的广播事件的客户端都得到我要广播的信息,这个实现应该不会有问题。但是现在我有一个消息只想通知 Mike 或 John (正如以上代码),(注:可能这时不能再称为“广播”了),我的广播程序依然将这个消息通知到了每一个客户端。

[1] [2] 下一页


文章地址:http://www.518web.net/bckf/aspnet/kfjq/15033.html

友情链接首页文字链接要求:PR≥3,搜索引擎正常收录,开通一年以上,内容健康充实的站点!申请...

Copyright(C) 2005-2008 518web.net,All Rights Reserve
Email:yojo.x@msn.com | 在线QQ:2306380 53614197| [浙ICP备08009643号]