Victor.Stone
dot net 备忘录
posts - 21,  comments - 25,  trackbacks - 0

服务端订阅客户端事件处理:(不能穿过防火墙,代理)

BI:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BI
{
    
public interface IBroadCast
    
{
        
event BroadCastEventHandler BroadCastEvent;
        
void BroadCastingInfo(string info);

        
void SetId(int id);
        
int GetId();
    }

}


using System;
using System.Collections.Generic;
using System.Text;

namespace BI
{
    
public delegate void BroadCastEventHandler(string msg);

    
public class EventWrapper : MarshalByRefObject
    
{
        
public event BroadCastEventHandler LocalBroadCastEvent;

        
//[OneWay]
        /// <summary>
        
/// 调用事件
        
/// </summary>

        public void BroadCasting(string message)
        
{
            LocalBroadCastEvent(message);
        }


        
public override object InitializeLifetimeService()
        
{
            
return null;
        }

    }



}



BS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BI;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;

namespace BS
{
    
public class BroadCast: MarshalByRefObject, IBroadCast
    
{
        
public event BroadCastEventHandler BroadCastEvent;

        [OneWay]
        
public void BroadCastingInfo(string info)
        
{
            
//if (BroadCastEvent != null)
            
//{
            
//    BroadCastEvent(info);
            
//}

            
if (BroadCastEvent != null)
            
{
                BroadCastEventHandler tempEvent 
= null;

                
int index = 1
                
//记录事件订阅者委托的索引,为方便标识,从1开始。
                
//防止一个客户端出错影响其它客户端
                foreach (Delegate del in BroadCastEvent.GetInvocationList())
                
{
                    
try
                    
{
                        tempEvent 
= (BroadCastEventHandler)del;
                        tempEvent(info);
                    }

                    
catch
                    
{
                        MessageBox.Show(
"事件订阅者" + index.ToString() + "发生错误,系统将取消事件订阅!");
                        BroadCastEvent 
-= tempEvent;
                    }

                    index
++;
                }

            }

            
else
            
{
                MessageBox.Show(
"事件未被订阅或订阅发生错误!");
            }

        }


        
private int id;
        
public void SetId(int id)
        
{
            
this.id = id;
        }


        
public int GetId()
        
{
            
return this.id;
        }


        
public override object InitializeLifetimeService()
        
{
            
return null;
        }

        
//#region IBroadCast 成员

        
//event BroadCastEventHandler IBroadCast.BroadCastEvent
        
//{
        
//    add { throw new NotImplementedException(); }
        
//    remove { throw new NotImplementedException(); }
        
//}

        
//void IBroadCast.BroadCastingInfo(string info)
        
//{
        
//    throw new NotImplementedException();
        
//}

        
//#endregion
    }

}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Collections;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;

namespace BS
{
    
public partial class BSFrm : Form
    
{
        
private BroadCast rmtObject; 
        
public BSFrm()
        
{
            InitializeComponent();
        }


        
private void BSFrm_Shown(object sender, EventArgs e)
        
{
            BinaryServerFormatterSinkProvider serverProvider 
= new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider 
= new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel 
= System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            IDictionary props 
= new Hashtable();
            props[
"useIpAddress"= false;
            props[
"machineName"= "localhost";
            props[
"port"= 9090;
            HttpChannel channel 
= new HttpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel, 
false);

            
this.rmtObject = new BroadCast();
            ObjRef objRef 
= RemotingServices.Marshal(rmtObject, "BroadCastMessage.soap"); 
        }


        
private void btnSend_Click(object sender, EventArgs e)
        
{
            
if (this.textBox1.Text != string.Empty)
            
{
                
this.rmtObject.BroadCastingInfo(this.textBox1.Text);
            }

            
else
            
{
                MessageBox.Show(
"请输入信息!");
            }


        }

    }

}



BC:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Runtime.Remoting.Channels.Http;
using BI;

namespace BC
{
    
public partial class BCFrm : Form
    
{
        IBroadCast watch;
        EventWrapper wrapper;

        
public BCFrm()
        
{
            InitializeComponent();
        }


        
private void BCFrm_Shown(object sender, EventArgs e)
        
{
            BinaryServerFormatterSinkProvider serverProvider 
= new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider 
= new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel 
= TypeFilterLevel.Full;

            IDictionary props 
= new Hashtable();
            
//props["bindTo"] = "121.12.157.244";//"113.105.185.151"; 
            props["port"= 0;
            HttpChannel channel 
= new HttpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel, 
false);

            
this.watch = (IBroadCast)Activator.GetObject(
                
typeof(IBroadCast),
                
"http://localhost:9090/BroadCastMessage.soap"); 

            
//watch.BroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
            this.wrapper = new EventWrapper();
            wrapper.LocalBroadCastEvent 
+= new BroadCastEventHandler(WriteLine);
            watch.BroadCastEvent 
+= new BroadCastEventHandler(wrapper.BroadCasting);
        }


        
private delegate void DelOutput(string str);

        
public void WriteLine(string message)
        
{
            
if (this.InvokeRequired)
            
{
                DelOutput output 
= new DelOutput(this.WriteLine);
                
this.Invoke(output, new object[] { message });
            }

            
else
            
{
                
this.txtMsg.Text += message + Environment.NewLine;
            }

        }


        
private void btnSet_Click(object sender, EventArgs e)
        
{
            
this.watch.SetId(Convert.ToInt32(this.txtSet.Text));
        }


        
private void btnGet_Click(object sender, EventArgs e)
        
{
            
this.txtGet.Text = this.watch.GetId().ToString();
        }


        
private void BCFrm_FormClosing(object sender, FormClosingEventArgs e)
        
{
            
//this.watch.BroadCastEvent -= new BroadCastEventHandler(this.wrapper.BroadCasting);
        }


    }

}

posted on 2010-12-16 09:05 Victor.Stone 阅读(586) 评论(0)  编辑 收藏 引用 所属分类: 分布式应用
只有注册用户登录后才能发表评论。

<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类(17)

文章分类

搜索

  •  

积分与排名

  • 积分 - 24226
  • 排名 - 193

最新随笔

阅读排行榜