Embedio: Implement WebSocket Routing

Created on 30 Dec 2017  路  10Comments  路  Source: unosquare/embedio

Does the websocket implementation also support the same routing rules. Ideally like...

server.RegisterModule(new WebSocketsModule());
server.Module<WebSocketsModule>().RegisterWebSocketsServer<WebSocketsChatServer>("/chat/{id}");

or just...

server.RegisterModule(new WebSocketsModule());
server.Module<WebSocketsModule>().RegisterWebSocketsServer<WebSocketsChatServer>("/chat/*");
enhancement help wanted

Most helpful comment

Great, not trying to be critical -- just trying to give constructive feedback to improve the project. Thanks.

All 10 comments

Hi @firesharkstudios , the current WebServer routes handler support both strategies. Let me take a quick look into WebSocketModuleif we can handle too.

@firesharkstudios did you test the source code or you need a pre-release nuget?

I'll pull the source

So, I created this bit of test code using the latest EmbedIO source and WebSocketSharp as the websocket client...

using System;
using System.Diagnostics;
using System.Net;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Modules;

namespace MyTest
{
    class Program {
        static void Main(string[] args) {
            using (var server = new WebServer("http://localhost:8000/", Unosquare.Labs.EmbedIO.Constants.RoutingStrategy.Regex)) {
                server.RegisterModule(new WebSocketsModule());
                server.Module<WebSocketsModule>().RegisterWebSocketsServer<TestWebSocketsServer>("/test/{id}");

                Task serverTask = server.RunAsync();

                using (var ws = new WebSocketSharp.WebSocket("ws://localhost:8000/test/123")) {
                    ws.Connect();
                }

            }
        }
    }

    public class TestWebSocketsServer : WebSocketsServer {

        public override string ServerName => "Test Server";

        protected override void OnClientConnected(WebSocketContext context, IPEndPoint localEndPoint, IPEndPoint remoteEndPoint) {
            context.RequestRegexUrlParams("/test/").TryGetValue("id", out object id);
            Debug.WriteLine($"id={id}");
        }

        protected override void OnClientDisconnected(WebSocketContext context) {
        }

        protected override void OnFrameReceived(WebSocketContext context, byte[] rxBuffer, WebSocketReceiveResult rxResult) {
        }

        protected override void OnMessageReceived(WebSocketContext context, byte[] rxBuffer, WebSocketReceiveResult rxResult) {
        }
    }
}

The OnClientConnected is invoked but the id is null instead of "123" as expected. I'm also really not clear what base path to pass into RequestRegexUrlParams(). What am I doing wrong to test this?

HI @firesharkstudios just use /test/{id} as base path in RequestRegexUrlParams().

Yes, that works. Nice job.

fyi -- I think if the param name was "urlPattern" instead of "baseUrl" that I would have guessed the value properly. The "base" prefix was throwing me off.

Yes you are right, sorry for the inconvenience, I'll fix this.

Great, not trying to be critical -- just trying to give constructive feedback to improve the project. Thanks.

A new EmbedIO version has just been released with these changes.

RequestRegexUrlParams() doesn't exist in 3.0 - how do we route in a websocket now?

Was this page helpful?
0 / 5 - 0 ratings