websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

AddWebSocketService() obsolete overload has no appropriate replacement

Open SoylentGraham opened this issue 7 years ago • 7 comments

	System.Func<ClientConnection> AllocService = () =>
	{
		return new ClientConnection(this);
	};
	Socket.AddWebSocketService<ClientConnection>("/", AllocService);

This overload is marked obsolete, but I can't see how to replicate the above; construct the behaviour with some parameters. I can only see parameterless-constructor options... Maybe I've misread the non-obsolete overloads...

FYI, the readme example uses this paradigm.

SoylentGraham avatar Mar 31 '18 13:03 SoylentGraham

I read this one as if you're supposed to use

AddWebSocketService<T>(string path, Action<T> initializer)

now. So you don't get to construct your own T, but to set it up..:

Socket.AddWebSocketService<ClientConection>("/", connection => connection.SetupThis(this));

daef avatar Apr 03 '18 12:04 daef

@daef Hi, I also have some problems with this. What variable is "connection" in your example? I don't seem to get it right. Thanks!

GianKiMoon avatar May 21 '18 09:05 GianKiMoon

@GianKiMoon connection is the parameter in the lambda, in c# you can remove some of the syntax. Slightly more verbose version of @daef 's example

System.Action<ClientConnection> SetupService = (Connection) =>
{
	Connection.SetupThis(this);
};
Socket.AddWebSocketService<ClientConection>("/", SetupService);

or

Socket.AddWebSocketService<ClientConection>("/", (connection) => { connection.SetupThis(this); } );

SoylentGraham avatar May 21 '18 09:05 SoylentGraham

@SoylentGraham Thank you! This makes it clear for me.

GianKiMoon avatar May 21 '18 09:05 GianKiMoon

@SoylentGraham @daef , hi, sorry to disturb, I still cannot get this right, System.Action<ClientConnection> SetupService = (Connection) => { Connection.SetupThis(this); }; Socket.AddWebSocketService<ClientConection>("/", SetupService);

is ClientConnection the class name inherit from WebSocketBehavior? What is function 'SetupThis' doing? Create new instance? how to implement it?

keepWalking avatar Dec 19 '19 10:12 keepWalking

oh, I managed to figure out the solution:

        this._server = new WebSocketServer(port);
        this._server.ReuseAddress = true;
        //this._server.AddWebSocketService<MyWsBehavior>("/");

        Action<MyWsBehavior> SetupService = this.AddBehaviorHandler;
        this._server.AddWebSocketService("/", SetupService);

    private void AddBehaviorHandler(MyWsBehavior wsBehavior)
    {
        wsBehavior.onCloseHandler += this.onConnectionClosed;
    }

    private void onConnectionClosed(object sender, EventArgs e)
    {
    }
        `

keepWalking avatar Dec 19 '19 14:12 keepWalking

    public WebSocketServer()
    {
        Action<WebSocketServer> initializer = Initializer;

        server = new(80);
        server.AddWebSocketService("/", initializer);
    }
        
    private void Initializer(WebSocketServer webSocketServer)
    {
        webSocketServer.OnAuthenticatingEvent += OnAuthenticatingEvent;
        webSocketServer.OnOpenEvent += OnOpenEvent;
        webSocketServer.OnCloseEvent += OnCloseEvent;
        webSocketServer.OnMessageEvent += OnMessageEvent;

        webSocketServer.OriginValidator = val => {
            return webSocketServer.OnAuthenticatingEvent?.Invoke(webSocketServer.Context.QueryString["token"]) ?? false;
        };

    }

richardags avatar Oct 20 '22 08:10 richardags