Code-Sharp/WampSharp

AspNetCoreWebSocketTransport

esso23 opened this issue · 6 comments

Hi,
AspNetCoreWebSocketTransport is not working for me.

I tried configuring it based on these issues.
#179
#216

Here is my configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            mHost =
                new WampHost
                (
                    new ForwardingRealmContainer("ws://10.8.0.250:50000/ws",
                        new TicketAuthenticator("Backend", "648g684g4864==[")),
                    new LooseUriValidator());

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

                mAspNetCoreWebSocketTransport = new AspNetCoreWebSocketTransport(builder);

                mHost.RegisterTransport(mAspNetCoreWebSocketTransport,
                    new JTokenJsonBinding(),
                    new JTokenMsgpackBinding());
            });

            app.UseHttpsRedirection();
            app.UseAuthentication();

            app.UseMiddleware<AuthMiddleware>();

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });
        }

First problem I found is that the connection is never opened, Open() method is never called so it keeps using EmptyHandler. Do I need to open it manually, or should that be handled somewhere in WampSharp? When I force WebSocketHandler in the constructor, it fails here (requests are properly sent from wss://xxx.xxx/ws to this handler):

IEnumerable<string> possibleSubProtocols =
                    context.WebSockets.WebSocketRequestedProtocols
                           .Intersect(this.SubProtocols);

string subprotocol =
                    possibleSubProtocols.FirstOrDefault();

because this.SubProtocols is empty.
I noticed that in RegisterTransport of WampHostBase here:

if (bindingArray.Any())
            {
                mTransportDefinitions.Add(new WampTransportDefinition()
                {
                    Transport = transport,
                    Bindings = bindingArray
                });
            }

the transport itself has no SubProtocols and empty mBindings after initialization.
At this point I stopped as there seems to be too many problems and I might be doing something wrong.

Any help would be appreciated
Esso

darkl commented

There are not enough details here, to debug this I need a full project that does not work. One common pitfall is to forget to call .Run() if you construct the Host yourself, see this issue.

Elad

darkl commented

I just used the example in the issue I referred to, and it works fine.
If you're using IIS, you might need to enable WebSockets.

Elad

Oh my god, I can't believe I spent so much time on this :D
Thanks a lot

Ok, I have another problem. When I test my application locally, everything works fine.
But when I deploy to IIS and host it on public domain, the request arrives, but context.WebSockets.IsWebSocketRequest is false.
debugview

darkl commented

You need to enable IIS WebSockets.

Elad

Thanks, leaving this here for any other IIS noobs like me :X (for Win 10):
Screenshot 2019-04-30 16 02 26