AddWebSocketService() obsolete overload has no appropriate replacement
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.
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 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 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 Thank you! This makes it clear for me.
@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?
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)
{
}
`
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;
};
}