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

Adding WebsocketService with lambda initialization does not work

Open Thowaah opened this issue 3 years ago • 1 comments

When trying to build this simple example from the README

public class Chat : WebSocketBehavior
{
    private string _suffix;

    public Chat()
      : this(null) {
    }

    public Chat(string suffix) {
        _suffix = suffix ?? String.Empty;
    }

    protected override void OnMessage(MessageEventArgs e) {
        Sessions.Broadcast(e.Data + _suffix);
    }
}

//...

private void CreateWebsocketServer() {
    websocketServer = new WebSocketServer(_wsServerPort);
    websocketServer.AddWebSocketService<Chat>("/ChatWithNyan", () => new Chat(" Nyan!"));
    websocketServer.Start();
}

I get this error on the lambda:

Delegate 'Action<Chat>' does not take 0 arguments

It does the same if I take Example2 and uncomment those lines:

wssv.AddWebSocketService<Chat>("/Chat", () =>new Chat("Anon#") {...}

If I do not use the initialization with the lambda, the example runs fine (see below).

private void CreateWebsocketServer() {
    websocketServer = new WebSocketServer(_wsServerPort);
    wssv.AddWebSocketService<Chat> ("/Chat");
    // websocketServer.AddWebSocketService<Chat>("/ChatWithNyan", () => new Chat(" Nyan!"));
    websocketServer.Start();
}

I will need the initialization to pass some data to my WebsocketService. Do someone have any idea of what could be going wrong ? Thanks.

Thowaah avatar Oct 25 '21 10:10 Thowaah

I was facing a similar issue until I did the following

websocketServer.AddWebSocketService<Chat>("/ChatWithNyan", p => new Chat(" Nyan!"));

Although this fixed the error, I can't get the params to be passed to the Service

PedroCavaleiro avatar Jan 05 '22 00:01 PedroCavaleiro