Giter Site home page Giter Site logo

ddpclient-net's Introduction

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("[email protected]", "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();
}

ddpclient-net's People

Contributors

johnnycrazy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.