无法在客户端/服务器应用程序上引发事件

时间:2012-06-17 19:23:26

标签: c# .net events client-server .net-remoting

我正在使用.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!");
}

0 个答案:

没有答案