sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.73k stars 1.66k forks source link

Adding WebsocketService with lambda initialization does not work #677

Open Thowaah opened 3 years ago

Thowaah commented 3 years ago

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.

PedroCavaleiro commented 2 years ago

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