日期:2008-03-23 浏览次数:20585 次
绝对于 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 {
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|