1

According to some samples in the internet and this guide I created a connection of webSocket .

public class sockets: IHttpHandler {

    public bool IsReusable {
        get {
            throw new NotImplementedException();
        }
    }
    public void ProcessRequest(HttpContext context) {
        if (context.IsWebSocketRequest) {
            context.AcceptWebSocketRequest(new socketHandler());
        }
    }
}

public class socketHandler: WebSocketHandler {

    public socketHandler(): base(null) {}
}

There is an error in the line-

context.AcceptWebSocketRequest(new socketHandler());

the error:

Argument 1: cannot convert from 'socketHandler' to 'System.Func(System.Web.WebSockets.AspNetWebSocketContext,System.Threading.Tasks.Task)'

Can anyone help me?

2 Answers 2

4

The AcceptWebSocketRequest takes a method as argument, not a class instance. You code should look something like this:

public void ProcessRequest(HttpContext context) {
    if (context.IsWebSocketRequest) {
        context.AcceptWebSocketRequest(HandleWebSocket);
    }
}

private Task HandleWebSocket(WebSocketContext wsContext)
{
    // Do something useful
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for helping..then the sample in the guide i linked to was wrong? however, in the socketHandler class I implemented the OnOpen, OnMessage etc functions...Now i dont understand when those functions will happen? and more important, when would the onOpen function in the javascript happen?
You should take a look at this SO question, one of the answers might suite your needs. stackoverflow.com/questions/25668398/…
2

You are referencing a function from System.Web while attempting to use a function from Microsoft.Web.WebSockets.
Add the appropriate reference and it will work.

1 Comment

Thanks that worked. The tutorial didn't seem to have a code download. This really helped me resolve the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.