Remoting模仿QQ实现客户端,服务器端聊天功能
来源:互联网 发布:淘宝国产手机 编辑:程序博客网 时间:2024/06/06 07:02
技术含量主要是用Remoting实现客户端订阅服务器端事件和服务器端订阅客户端事件,至于客户端和客户端聊天,那就是用的这两种技术的结合体了。两个DLL原代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace YiRen.Qq.Commol
{
//公共事件委托模型
public delegate void CommandHandle(string txt);
public interface IQq
{
event CommandHandle ChuFaQq;
void Send(string txt);
}
///<summary>
///客户端事件中间类
///</summary>
public class CenterClass : MarshalByRefObject
{
public event CommandHandle CenterQq;
public void Fax(string txt)
{
CenterQq(txt); //触发事件类,以此事件来更新客户端消息
}
/// <summary>
/// 继承此方法来保证事件中间客户类生命周期永远保持
/// </summary>
/// <returns></returns>
public override object InitializeLifetimeService()
{
return null;
}
}
public interface IClient
{
void ClientSend(string txt);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using YiRen.Qq.Commol;
using System.Windows.Forms;
namespace YiRen.Qq.YuanCheng
{
/// <summary>
/// 远程对象类,利用此类来群发客户端消息
/// </summary>
public class ServerClass : MarshalByRefObject, IQq
{
public event CommandHandle ChuFaQq;
//检查委托链来防止客户端突然断电等远程造成系统故障
public void Send(string txt)
{
if (ChuFaQq != null)
{
CommandHandle temp = null;
int num = 1; //用来记录哪个客户端出故障
try
{
foreach (Delegate dele in ChuFaQq.GetInvocationList())
{
temp = (CommandHandle)dele;
temp(txt);
}
}
catch
{
MessageBox.Show("客户端" + num.ToString() + "出故障,请检查");
num++;
}
}
}
public override object InitializeLifetimeService()
{
return null;
}
}
public class CClient : MarshalByRefObject, IClient
{
public static event CommandHandle Client;
public void ClientSend(string txt)
{
if (Client != null)
{
Client(txt);
}
}
public override object InitializeLifetimeService()
{
return null;
}
}
}
服务器端原代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YiRen.Qq.Commol;
using YiRen.Qq.YuanCheng;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Threading;
namespace WindowsApplication1
{
public partial class frmserver : Form
{
public frmserver()
{
InitializeComponent();
this.button1.Click += new EventHandler(button1_Click);
}
private ServerClass server = null;
void button1_Click(object sender, EventArgs e)
{
if (this.textBox2.Text!= string.Empty)
{
this.textBox1.Text += this.textBox2.Text + "\r\n";
server.Send(this.textBox2.Text.ToString());
this.textBox2.Text = "";
}
}
private void frmserver_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider ser = new BinaryServerFormatterSinkProvider();
ser.TypeFilterLevel = TypeFilterLevel.Full; //需要传送委托事件,所以必须用满格序列化
BinaryClientFormatterSinkProvider cli = new BinaryClientFormatterSinkProvider();
IDictionary wo = new Hashtable();
wo["port"] = "2000";
TcpChannel tcp = new TcpChannel(wo, cli, ser);
ChannelServices.RegisterChannel(tcp);
server = new ServerClass();
ObjRef obj =RemotingServices.Marshal(server, "Qq");
//模仿客户端群发事件的远程类
HttpChannel http = new HttpChannel(2002);
ChannelServices.RegisterChannel(http);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CClient), "Client", WellKnownObjectMode.Singleton);
CClient.Client += new CommandHandle(CClient_Client);
}
void CClient_Client(string txt)
{
this.textBox1.BeginInvoke(new MyDele(Shu), txt);
server.Send(txt.ToString());
}
delegate void MyDele(string txt);
private void Shu(string txt)
{
this.textBox1.Text += txt + "\r\n";
}
}
}
- Remoting模仿QQ实现客户端,服务器端聊天功能
- 简单迭代服务器端/客户端:模仿飞Q聊天功能,实现IM通信
- 用服务器端和客户端来实现聊天功能
- 网络编程UDP模仿QQ聊天功能
- 模仿QQ聊天
- 利用socket实现客户端与服务器端聊天
- Socket——实现简单的一对一服务器端与客户端聊天功能
- 直播项目,在线聊天功能--socket实现客户端和服务器端通讯的简单案例
- MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能...
- 在Linux下用多线程实现两台客户端通过服务器进行聊天,类似qq功能
- 一个简易聊天功能的服务器端和客户端源码
- 浏览器 模仿QQ聊天 对话框
- QQ聊天功能在局域网中的实现
- Android仿QQ实现聊天功能
- android客户端 j2ee服务器端架构的聊天系统实现思路
- 模仿QQ空间的下拉更新的功能实现
- .NET Remoting服务器端订阅客户端事件
- Qt模仿安卓QQ聊天界面
- 用 StringTemplate重新制作代码生成工具
- C#深度优先遍历结构算法
- 如何手动向datagridview添加数据
- C#利用PropertyInfo反射类实现二个类属性值的比较
- C#开发ActiveX控件及制作CAB包
- Remoting模仿QQ实现客户端,服务器端聊天功能
- 在Winform应用程序中精确切换输入法
- 在Winform应用程序中精确切换输入法(续)
- C#播放声音的四种方法
- 使用配置文件(.settings、.config)存储应用程序配置
- 开源Perfect
- CSS 使用百分比布局
- 配置好你的vim-《别怕Linux编程》之一
- 深搜_Poj_2488(字典序)