我正在使用 .NET Remoting 实现多人游戏。客户端连接到服务器,我可以玩游戏了。但是,连接等没有问题;我在客户端订阅了事件,当事件被触发时,什么也没有发生。
服务器端网络实现是这样的:
SetupTcpChannel();
RemotingConfiguration.RegisterWellKnownServiceType(typeof(GamePlay),
"MyGame.soap", WellKnownObjectMode.Singleton);
RemotingConfiguration.CustomErrorsEnabled(false);
客户端:
SetupTcpChannel();
var myGame = (IGamePlay)Activator.GetObject(typeof(IGamePlay), "tcp://localhost:13101/MyGame.soap");
private void SetupTcpChannel()
{
// Creating a custom formatter for a TcpChannel sink chain.
BinaryServerFormatterSinkProvider server_provider = new BinaryServerFormatterSinkProvider();
server_provider.TypeFilterLevel = Formatters.TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider client_provider = new BinaryClientFormatterSinkProvider();
// Creating the IDictionary to instantiate the channel with custom sinkprovider
IDictionary properties = new Hashtable();
properties["port"] = "0"; //let the system choose the portnumber to prevent double portnumbers with multiple clients
// Pass the properties for the port setting and the server provider in the server chain argument. (Client remains null here.)
TcpChannel channel = new TcpChannel(properties, client_provider, server_provider);
ChannelServices.RegisterChannel(channel, false);
}
IGamePlay 接口中的委托和事件:
public delegate void getMessageDel();
event getMessageDel event_message;
GamePlay 类中的函数:
public void function() {
if (event_game_started != null)
event_game_started();
}
在客户端订阅事件:
myGame.event_game_is_over += new gameIsOverDel(myGame_event_game_is_over);
void myGame_event_game_is_over()
{
MessageBox.Show("Game is over!");
}