← Back to all articles

Finding a game of Doom with Rx

Thanks to everyone for coming to our Rx presentation last night and thanks to Mitch for running the most isolated (in terms of distance) .NET community in the world! Towards the end of the presentation we developed an application that was functionally similar to this;

Source Code: http://www.amnet.net.au/~jamesmiles/FindGame.zip

image

Our service (internet) interface looked like this;

    public interface IInternet
    {
        IObservable Ping(string hostname);
        IObservable> GetServers(string query);
    }

Of most interest was the following query (I’ve split in two so that it’s easier to understand).

    // Get rows observable
    private IObservable GetRows(string text)
    {
        var query =
            from servers in _internet.GetServers(text)
            from server in servers.ToObservable()
            from latency in Ping(server.Hostname)
            select new Row
            {
                Hostname = server.Hostname,
                IpAddress = server.IpAddress,
                Latency = latency,
            };

        return query.ObserveOnDispatcher();
    }

    // Get ping observable
    private IObservable Ping(string hostname)
    {
        return _internet.Ping(hostname)
            .Select(x => x + " ms")
            .Timeout(TimeSpan.FromSeconds(1), Observable.Return("timed-out"));
    }

I think this application demonstrates the flexibility of Rx. It’s not just about handling market data & ticking prices. I think games in general are a great application of Rx, but that’s for another day.

Please contact me if you’d like additional information.

Comments