Comments (5)
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.
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.
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.
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.
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)
- How to handle wamp.error.procedure_already_exists when callee reconnects? HOT 4
- Cache fallback layer to RPC call HOT 1
- How to limit the websocket frame size? HOT 16
- Getting query string parameters with asp.net core. HOT 4
- Request to upgrade to .net 5.1 to fix Websocket issues HOT 2
- Modify WampTopics at Runtime? HOT 4
- Add inner exception in ConvertExceptionToRuntimeException HOT 6
- Windows forms app problem HOT 1
- Publishing to topic in .SubscriptionAdded seems to be impossible? HOT 6
- Hello, I am new to the internet HOT 5
- WampCallee memory leak on InvocationData? HOT 6
- Event handler leak when cancel an invocation
- Possible to have different types for IProgress<T> and call result in reflection based callee? HOT 10
- How do I get the IP of a client in the session created event on the router? HOT 2
- Progress reporting broken for async operations without a return value HOT 2
- High amount of memory usage from ImmutableList<Int64> HOT 1
- Connection Error Issues HOT 1
- Unhandled terminating exception due to async void HOT 6
- Subscriptions don't work after reconnection. How to handle `onError` with Reflection-based subscriber? HOT 8
- Unable to open channel, WebAssembly, client-side code HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wampsharp.