What is a WebSocketAccept?
WebSocketAccept is using an alias
Example:
... using Foo.Bar using MyBar = Fee.Bar ...
Here we use Bar from two different namespaces, but we, like the second, have a " MyBar ", so we can distinguish between the two.
Why use an alias like WebSocketAccept?
An alias in this case is just a convenience, so you do not need to enter all of this, which means that instead of writing the entire name when using it, you can use an alias instead.
WebSocketAccept Overview
If we look closer, we will see that type:
Action<A, B>
This means that it is essentially a function that does not return and takes 2 arguments, in C # lambda:
(A, B) => { }
We see that the first argument is (A): IDictionary<string, object> , which is also known as the Owin framework.
The second argument is (B): Func<C, D> , which means that it is a function that takes C and returns a D In C # lambda:
(C) => { return D; }
Then we need to dive into the 1st argument (C) of the second argument (B). And we see that it accepts the Owin environment and returns Task .
What accepts?
accept tries to extract the parameters from the IOwinContext and map them to the WebSocketAccept type.
If he cannot extract them, he is null , and we will move on to the next middleware.
Otherwise, it was a websocket request, and we call a function that takes 2 parameters ( WebSocketAccept ), as discussed above ( Action<A, B> ).
The first parameter is a regular dictionary containing websocket acceptance parameters.
The second parameter is a function that takes a dictionary and returns a task.
This function is called by someone else, which makes the code pass the callback function along with the caller.
Then the caller calls the function with the correct argument. Because the caller knows the signature of the function. This function is called AFTER receiving a request to connect to a web server. Hence the comment callback.
What if we want to pass this down the pipeline as soon as we determine that this is a web socket request?
In this example, the WebSocketEcho callback function, but essentially you can pass any function that satisfies the function signature:
Task MyCallbackFunction(IDictionary<string, object> context) {
The conclusion is that you are not calling the function, the function is being called for you. You indicate that after negotiating the connection with the web socket request, you decide what happens.
The WebSocketEcho function is called once for each client and a loop is executed until the client wants to close the connection. In the meantime, he drives away everything he receives.
Disclaimer: I also just try to wrap my head around web sockets and owin, but I wanted to share my findings for posterity, since no one answered your question. I welcome any corrections.
EDIT
I noticed in my own experiment that if you return from the callback function that the websocketContext connection will be Abort ed. This means that you cannot send / receive messages on the connection if you pass the websocketContext after the callback completes.
UPDATE
The last time I tried to use this on the IIS 7.5 server for Windows 2008 R2, I could not get the webcam to work. Then, according to this: fooobar.com/questions/217607 / ... - IIS 7.5 server does not support websites.
This means that if your application is hosted in IIS 7.5, it will not be able to have websites.
Then I thought about a possible solution:
- Use a separate application, for example. A utility program (outside of IIS) that processes websocket requests.
- Use reverse proxy to match request with service application
It was too cumbersome for me, which made me postpone websocket implementation for now ...