Skip to content

A Client for Meteor's DDP-Protocol written in C#/.NET

License

Notifications You must be signed in to change notification settings

rickard-505/DDPClient-NET

 
 

Repository files navigation

DDPClient-NET Build status

A Client for Meteor's DDP-Protocol written in C#/.NET

##WIP This library is currently Work-In-Progress. Further Improvements, documentation + Unit-Tests will follow soon.

##Usage ###Connecting

static void Main(string [] args)
{
    //URL to your Meteor location
    _client = new DdpConnection("localhost:3000");
    _client.Retry = true; //Retry if we lose connection or initial connection fails
    _client.Login += OnLogin; //Login Callback
    _client.Connected += OnConnected;
    _client.Connect();

    Console.ReadKey();
    _client.Close(); //Close when we're done
}

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    if(connectResponse.DidFail()) //We're using a not supported DDP Version
        Console.WriteLine("Connecting Failed, Server wants Version: " + connectResponse.Failed.Version);

    //The client will save this ID and use it for reconnection attempts automatically
    Console.WriteLine("Connected! Our Session-ID: " + connectResponse.Session);
}

###Login

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    //Should check if successful


    _client.LoginWithEmail("test@test.de", "password");
    //or
    _client.LoginWithUsername("username", "password");
    //or
    _client.LoginWithToken("token");    
}

private static void Login(object sender, LoginResponse loginResponse)
{
    if(loginResponse.HasError())
        Console.WriteLine(loginResponse.Error.Error);
    Console.WriteLine("Success: " + loginResponse.Token);
    Console.WriteLine("Expires In:" + loginResponse.TokenExpires.DateTime);

    //You can save the token and use it later with LoginWithToken (until TokenExpires)
}

###Methods ####No Return-Type

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    //Should check if successful


     _client.Call("test"); //No parameter
     _client.Call("test", 5); //Single parameter
     _client.Call("test", 5, false, new Task()); //Multiple parameters
}

####Dynamic Return-Type

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    //Should check if successful


    _client.Call("test", (response) =>
    {
        if(response.HasError())
            return; //Print Error...
        Console.WriteLine(response.Result); //Result is dynamic
        //or
        Console.WriteLine(response.Get<Task>().Name); //Convert it to some type (Json.NET)
    });
}

####Fixed Return-Type

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    //Should check if successful


    _client.Call<Task>("test", (error, result) =>
    {
        if(error != null)
            return; //Print Error...
        Console.WriteLine(result.Name); //Result is dynamic
    });
}

###Collections/Subscribers ####Event-Based

static void Main(string [] args)
{
    //After connecting...

    DdpSubscriber<Task> subscriber = _client.GetSubscriber<Task>("task");
    subscriber.Added += (o, addedModel) => Console.WriteLine("Added: " + addedModel.Object.Name);
    subscriber.Removed += (o, removedModel) => Console.WriteLine("Removed ID: " + removedModel.Id);

    //Should close here
}

####Interface-Based

static void Main(string [] args)
{
    //After connecting...

    DdpSubscriber<Task> subscriber = _client.GetSubscriber<Task>("task");
    subscriber.Subscribers.Add(new TaskSubscriber());
    //Should close here
}

internal class TaskSubscriber : IDdpSubscriber<Task>
{
    public void Added(SubAddedModel<Task> added)
    {

    }
    //...
}

###Subscriptions

private static void OnConnected(object sender, ConnectResponse connectResponse)
{
    //Should check if successful

    DdpSubHandler subHandler = _client.GetSubHandler("testSub");
    subHandler.Ready += (o, args) => Console.WriteLine("Sub Ready");
    subHandler.Sub();

    //If you dont want any data: subHandler.Unsub();
}

About

A Client for Meteor's DDP-Protocol written in C#/.NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%