Giter VIP home page Giter VIP logo

Comments (5)

darkl avatar darkl commented on August 20, 2024

Hi @abettadapur
WAMP2 support is still in development.
You can try the dev branch available here:
https://github.com/Code-Sharp/WampSharp/tree/wampv2

See the README file for instructions how to install from NuGet.
See the src/net45/Samples/WAMP2 directory for callee/subscriber examples. (Publisher isn't implemented yet, Caller api is available, but will be improved. No samples available)

If you have further questions you are welcome.
Please note that this is still under development and things are going to be changed in the upcoming released version.

from wampsharp.

abettadapur avatar abettadapur commented on August 20, 2024

Thanks! If its not too much trouble, would you mind giving me a brief explanation of the Caller API?
In the previous API I created an interface and used a proxy and then called the methods from the interface. Is the process similar?

from wampsharp.

darkl avatar darkl commented on August 20, 2024

Currently this feature isn't implemented yet (it will be when this is released, see issue #26 ).

At this moment, the api is more "low level":

internal class Program
{
    private static void Main(string[] args)
    {
        string serverAddress = "ws://127.0.0.1:9090/ws";

        JTokenBinding binding = new JTokenBinding();

        WampChannelFactory<JToken> factory =
            new WampChannelFactory<JToken>(binding);

        WampChannel<JToken> channel =
            factory.CreateChannel("realm1",
                                  new WebSocket4NetTextConnection<JToken>(serverAddress,
                                                                          binding));

        Task task = channel.Open();
        task.Wait(5000);
        var dummy = new Dictionary<string, string>();

        MyCallback callback = new MyCallback();
        channel.RealmProxy.RpcCatalog.Invoke(callback, dummy, "com.arguments.add2", new object[] {3, 4});

        int seven = callback.Task.Result;

        Console.ReadLine();
    }

    internal class MyCallback : IWampRawRpcOperationCallback
    {
        private readonly TaskCompletionSource<int> mTask = new TaskCompletionSource<int>();

        public Task<int> Task
        {
            get { return mTask.Task; }
        }


        public void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details)
        {
            throw new NotImplementedException();
        }

        public void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments)
        {
            int result = formatter.Deserialize<int>(arguments[0]);
            mTask.SetResult(result);
        }

        public void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments,
                                     TMessage argumentsKeywords)
        {
            int result = formatter.Deserialize<int>(arguments[0]);
            mTask.SetResult(result);
        }

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error)
        {
            throw new NotImplementedException();
        }

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error,
                                    TMessage[] arguments)
        {
            IDictionary<string, object> deserializedDetails =
                formatter.Deserialize<IDictionary<string, object>>(details);

            object[] deserializedArguments = arguments.Cast<object>().ToArray();

            WampException exception = new WampException
                (deserializedDetails,
                 error,
                 deserializedArguments);

            mTask.SetException(exception);
        }

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error,
                                    TMessage[] arguments,
                                    TMessage argumentsKeywords)
        {
            IDictionary<string, object> deserializedDetails =
                formatter.Deserialize<IDictionary<string, object>>(details);

            object[] deserializedArguments = arguments.Cast<object>().ToArray();

            IDictionary<string, object> deserializedArgumentsKeywords =
                formatter.Deserialize<IDictionary<string, object>>(argumentsKeywords);

            WampException exception = new WampException
                (deserializedDetails,
                 error,
                 deserializedArguments,
                 deserializedArgumentsKeywords);

            mTask.SetException(exception);
        }
    }
}

After issue #26 is implemented, you won't need all the callback code, it will happen magically beyond the scenes. All you'll need to do is to declare an interface.
But it will take me some time to implement this, it will definitely won't happen in this week.

from wampsharp.

darkl avatar darkl commented on August 20, 2024

Until the feature is implemented, I can recommend you to create the proxies class manually:
(Dirty code)

public interface IArgumentsService
{
    [WampProcedure("com.arguments.ping")]
    Task PingAsync();

    [WampProcedure("com.arguments.add2")]
    Task<int> Add2Async(int a, int b);

    [WampProcedure("com.arguments.orders")]
    Task<string[]> OrdersAsync(string product, int limit = 5);

    [WampProcedure("com.arguments.ping")]
    void Ping();

    [WampProcedure("com.arguments.add2")]
    int Add2(int a, int b);

    [WampProcedure("com.arguments.orders")]
    string[] Orders(string product, int limit = 5);
}

internal class ArgumentsServiceProxy : IArgumentsService
{
    private readonly IWampRpcOperationCatalogProxy mCatalogProxy;
    private Dictionary<string, object> mDummy = new Dictionary<string, object>();

    public ArgumentsServiceProxy(IWampRpcOperationCatalogProxy catalogProxy)
        {
            mCatalogProxy = catalogProxy;
        }

    public Task PingAsync()
        {
            PingCallback callback = new PingCallback();
            mCatalogProxy.Invoke(callback, mDummy, "com.arguments.ping");
            return callback.Task;
        }

    public Task<int> Add2Async(int a, int b)
        {
            AddCallback callback = new AddCallback();
            mCatalogProxy.Invoke(callback, mDummy, "com.arguments.add2", new object[] { a, b });
            return callback.Task;
        }

    public Task<string[]> OrdersAsync(string product, int limit = 5)
        {
            OrdersCallback callback = new OrdersCallback();
            mCatalogProxy.Invoke(callback, mDummy, "com.arguments.orders", new object[] { product, limit });
            return callback.Task;
        }

    public void Ping()
        {
            this.PingAsync().Wait();
        }

    public int Add2(int a, int b)
        {
            return this.Add2Async(a, b).Result;
        }

    public string[] Orders(string product, int limit = 5)
        {
            return this.OrdersAsync(product, limit).Result;
        }

    private abstract class Callback<T> : IWampRawRpcOperationCallback
    {
        protected readonly TaskCompletionSource<T> mTask = new TaskCompletionSource<T>();

        public Task<T> Task
            {
                get { return mTask.Task; }
            }

        public abstract void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details);

        public abstract void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details,
                                              TMessage[] arguments);

        public abstract void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details,
                                              TMessage[] arguments,
                                              TMessage argumentsKeywords);

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error)
            {
                IDictionary<string, object> deserializedDetails =
                    formatter.Deserialize<IDictionary<string, object>>(details);

                WampException exception = new WampException
                    (deserializedDetails,
                     error);

                mTask.SetException(exception);
            }

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error,
                                    TMessage[] arguments)
            {
                IDictionary<string, object> deserializedDetails =
                    formatter.Deserialize<IDictionary<string, object>>(details);

                object[] deserializedArguments = arguments.Cast<object>().ToArray();

                WampException exception = new WampException
                    (deserializedDetails,
                     error,
                     deserializedArguments);

                mTask.SetException(exception);
            }

        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error,
                                    TMessage[] arguments,
                                    TMessage argumentsKeywords)
            {
                IDictionary<string, object> deserializedDetails =
                    formatter.Deserialize<IDictionary<string, object>>(details);

                object[] deserializedArguments = arguments.Cast<object>().ToArray();

                IDictionary<string, object> deserializedArgumentsKeywords =
                    formatter.Deserialize<IDictionary<string, object>>(argumentsKeywords);

                WampException exception = new WampException
                    (deserializedDetails,
                     error,
                     deserializedArguments,
                     deserializedArgumentsKeywords);

                mTask.SetException(exception);
            }
    }

    private class PingCallback : Callback<bool>
    {
        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details)
            {
                mTask.SetResult(true);
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments)
            {
                mTask.SetResult(true);
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments, TMessage argumentsKeywords)
            {
                mTask.SetResult(true);
            }
    }

    private class AddCallback : Callback<int>
    {
        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details)
            {
                throw new NotImplementedException();
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details,
                                              TMessage[] arguments)
            {
                int result = formatter.Deserialize<int>(arguments[0]);
                mTask.SetResult(result);
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details,
                                              TMessage[] arguments,
                                              TMessage argumentsKeywords)
            {
                int result = formatter.Deserialize<int>(arguments[0]);
                mTask.SetResult(result);
            }
    }

    class OrdersCallback : Callback<string[]>
    {
        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details)
            {
                throw new NotImplementedException();
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments)
            {
                string[] result =
                    arguments.Select(x => formatter.Deserialize<string>(x))
                             .ToArray();

                mTask.SetResult(result);
            }

        public override void Result<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, TMessage[] arguments, TMessage argumentsKeywords)
            {
                string[] result =
                    arguments.Select(x => formatter.Deserialize<string>(x))
                             .ToArray();

                mTask.SetResult(result);
            }
    }

}

Usage:

private static void Main(string[] args)
{
    string serverAddress = "ws://127.0.0.1:9090/ws";

    JTokenBinding binding = new JTokenBinding();

    WampChannelFactory<JToken> factory =
        new WampChannelFactory<JToken>(binding);

    WampChannel<JToken> channel =
        factory.CreateChannel("realm1",
                              new WebSocket4NetTextConnection<JToken>(serverAddress,
                                                                      binding));

    Task task = channel.Open();
    task.Wait(5000);

    IArgumentsService proxy = new ArgumentsServiceProxy(channel.RealmProxy.RpcCatalog);

    Task pong = proxy.PingAsync();
    Task<int> asyncSeven = proxy.Add2Async(3, 4);
    Task<string[]> asyncOrders = proxy.OrdersAsync("Book", 4);
}

The api will be improved of course when WampSharp WAMPv2 version is released.

from wampsharp.

darkl avatar darkl commented on August 20, 2024

Hi @abettadapur

I've implemented this feature almost completely (out/ref parameters support isn't available yet).

You can test it if you like, here is an example code:

DefaultWampChannelFactory factory =
    new DefaultWampChannelFactory();

string serverAddress = "ws://127.0.0.1:8080/ws";

IWampChannel channel =
    factory.CreateJsonChannel(serverAddress, "realm1");

channel.Open().Wait(5000);

IArgumentsService proxy = 
    channel.RealmProxy.Services.GetCalleeProxy<IArgumentsService>();

proxy.Ping();
Console.WriteLine("Pinged!");

int result = proxy.Add2(2, 3);
Console.WriteLine("Add2: {0}", result);

var starred = proxy.Stars();
Console.WriteLine("Starred 1: {0}", starred);

starred = proxy.Stars(nick: "Homer");
Console.WriteLine("Starred 2: {0}", starred);

starred = proxy.Stars(stars: 5);
Console.WriteLine("Starred 3: {0}", starred);

starred = proxy.Stars(nick: "Homer", stars: 5);
Console.WriteLine("Starred 4: {0}", starred);

string[] orders = proxy.Orders("coffee");
Console.WriteLine("Orders 1: {0}", string.Join(", ", orders));

orders = proxy.Orders("coffee", limit: 10);
Console.WriteLine("Orders 2: {0}", string.Join(", ", orders));

from wampsharp.

Related Issues (20)

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.