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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Adi Malachi
  • 69
  • 2
  • 6

2 Answers2

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
}
Pieter
  • 103
  • 4
  • 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? – Adi Malachi Jun 19 '15 at 07:43
  • You should take a look at this SO question, one of the answers might suite your needs. http://stackoverflow.com/questions/25668398/using-websockets-with-asp-net-web-api – Pieter Jun 19 '15 at 13:42
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.

MosheG
  • 178
  • 1
  • 11