net remoting 实现简易的控制台命令行聊天室
本套程序由四个主要的对象组成:
1. chatroom (chatroom.cs): 服务器端真正的提供远程服务的对象,负责以服务器推(push)的方式把 sender 发来的消息"广播"出去。
/*
csc.exe chatroom.cs /t:library chatroom.dll
*/
using system;
using system.runtime.remoting;
[serializable]
public class chatroom : marshalbyrefobject
{
//定义了 1 个名为 "chatroomeventhandler 的事件处理委托" 及其参数格式签名
public delegate void chatroomeventhandler(string s);
//定义了 3 个 "chatroomeventhandler 委托类型" 的事件及远程回调函数
public event chatroomeventhandler messagereceive; //消息接收事件
public event chatroomeventhandler login; //登录事件
public event chatroomeventhandler logoff; //退出事件
public void onmessagereceive(string message)
{
if (messagereceive != null)
{
//触发 receiver 客户端 messagereceive 事件,广播所有消息
messagereceive(message);
}
console.writeline("server: " + message); //服务器消息监视
}
public void onlogin(string user)
{
if (login != null)
{
//触发 receiver 客户端 login 事件,广播 "登录" 消息
login("system say: " + user + " login!");
}
console.writeline("server: " + user + " login!");
}
public void onlogoff(string user)
{
if (logoff != null)
{
//触发 receiver 客户端 logoff 事件,广播 "退出" 消息
logoff("system say: " + user + " logoff!");
}
console.writeline("server: " + user + " logoff!");
}
public override object initializelifetimeservice()
{
return null;
}
}
2. server: 服务器。远程服务对象的宿主程序而已。
/*
csc.exe server.cs
*/
using system;
using system.runtime.remoting;
class server
{
public static void main(string[] args)
{
remotingconfiguration.configure("s.config");
console.writeline("server .... , press enter key to exit.");
console.readline();
}
}
下面是 server 的配置文件 (s.config):
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="singleton" type="chatroom,chatroom" objecturi="chatroomurl" />
</service>
<channels>
<channel ref="http" port="8080">
<!-- 注意 设置反序列化级别 -->
<serverproviders>
<provider ref="wsdl" />
<formatter ref="soap" typefilterlevel="full" />
<formatter ref="binary" typefilterlevel="full" />
</serverproviders>
<clientproviders>
<formatter ref="binary" />
</clientproviders>
<!-- 注意 设置反序列化级别 -->
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
3. sender (sender.cs): 客户端消息发送器对象,负责把"广播"的消息发送到远程服务对象。
/*
csc.exe /r:chatroom.dll sender.cs
*/
using system;
using system.timers;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.http;
class sender
{
chatroom x;
public static void main(string[] args)
{
sender y = new sender();
y.run();
}
string user;
public void run()
{
console.writeline("messages sender ... ,press 'q' to exit chatting.");
// 以配置方式获取远程服务对象实例
// remotingconfiguration.configure("c.config");
// x = new chatroom();
//以编程方式获取远程服务对象实例
system.runtime.remoting.channels.channelservices.registerchannel(new system.runtime.remoting.channels.http.httpchannel());
x = (chatroom) system.activator.getobject(typeof(chatroom),"http://server:8080/chatroomurl");
//先登录
console.writeline("make a name then login please:");
user = console.readline();
//调用此远程方法,通知服务器触发 receiver 客户端 login 事件,广播 "登录" 消息