Code-Sharp / WampSharp

A C# implementation of WAMP (The Web Application Messaging Protocol)
http://wampsharp.net
Other
385 stars 84 forks source link

Getting query string parameters with asp.net core. #335

Open Dawiducik opened 3 years ago

Dawiducik commented 3 years ago

Hello, My goal is to connect to Caller with path e.g. ws://localhost:8081/ws, but I want to provide additional GET paramer to URL (so it becomes more like ws://localhost:8081/ws/<resource-id>) and e.g. reject the connection. My question is, is it possible in current state of this library? Is there any DI service containing context? Maybe I can use HttpContext in handlers? Should I make my own workaround, or use WAMP host in controller method taking GET params? Thanks in advance!

darkl commented 3 years ago

This is how setting up WampSharp in ASP.NET Core looks like:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    WampHost host = new WampHost();

    app.Map("/ws", builder =>
    {
        builder.UseWebSockets();

        host.RegisterTransport(new AspNetCoreWebSocketTransport(builder),
                               new JTokenJsonBinding(),
                               new JTokenMsgpackBinding());
    });

    host.Open();
}

Since you pass the builder to the AspNetCoreWebSocketTransport ctor, you are able to modify it beforehand and for instance do some address filtering there. Does that help with your question?

Elad

Dawiducik commented 3 years ago

Well, not really, I know how to configure and register "Caller" or "Calee" but I still can't figure out how to retrieve query parameters from http context in the way like you did in the example.

darkl commented 3 years ago

Can you describe a simple example of what you are willing to achieve?

Elad

On Tue, Jun 22, 2021, 13:06 Dawid Rzeszutek @.***> wrote:

Well, not really, I know how to configure and register "Caller" or "Calee" but I still can't figure out how to retrieve query parameters from http context in the way like you did in the example.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Code-Sharp/WampSharp/issues/335#issuecomment-866169728, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIS75VFVQHC4EZ3GRB2GTLTUC7LFANCNFSM47BYNJJQ .

Dawiducik commented 3 years ago

First of all I think I misled you a little bit, I want to expose CALLEE, not CALLER.

So lets assume that I have super simple service ping-ponging the server and client.

public interface IArgumentsService
{
    [WampProcedure("com.arguments.ping")]
    string Ping();
}

public class ArgumentsService : IArgumentsService
{
    public string Ping()
    {
        return "pong";
    }
}

And my Startup.cs looks like this:

var wampHost = new WampHost();
app.Map("/ws/<id>", async builder =>
{
    builder.UseWebSockets();
    var realm = wampHost.RealmContainer.GetRealmByName("realm1");
    var instance = new ArgumentsService();
    await realm.Services.RegisterCallee(instance);
    // Get "id" from query string, but how?
    // Accept the connection, but send the error based on resource id.
    wampHost.RegisterTransport(new AspNetCoreWebSocketTransport(builder),
                        new JTokenJsonBinding(),
                        new JTokenMsgpackBinding());
});
wampHost.Open();

How may I achieve this? And the second thing, how may I attach the WAMP host to a different service through DI, and send the message to specific websocket connected with given id?