c# - Owin Websockets - Understanding IOwinContext and WebSocketAccept -


reading through here , looking @ example here:

i trying understand websocketaccept does. know websocketaccept is:

using websocketaccept =     action     <         idictionary<string, object>, // websocket accept parameters         func // websocketfunc callback         <             idictionary<string, object>, // websocket environment             task // complete         >     >; 

and used in manner:

public void configuration(iappbuilder app)     {         app.use(upgradetowebsockets);         app.usewelcomepage();     }      // run once per request     private task upgradetowebsockets(iowincontext context, func<task> next)     {         websocketaccept accept = context.get<websocketaccept>("websocket.accept");         if (accept == null)         {             // not websocket request             return next();         }          accept(null, websocketecho);          return task.fromresult<object>(null);     } 

so accept() actuallyt doing? calling func<> property of websocketaccept , method websocketecho defined? websocketecho defined as:

  private async task websocketecho(idictionary<string, object> websocketcontext) 

so websocketcontext come from? if wanted pass further down pipeline once have identified web socket request?

what websocketaccept?

websocketaccept using alias

example:

... using foo.bar using mybar = fee.bar ... 

here using bar 2 different namespaces, alias 2nd 1 'mybar' can differentiate between two.

why use alias websocketaccept?

the alias in case convenience, don't have type whole thing, means instead of writing whole name when using it, can use alias instead.

understanding websocketaccept

if closer see type is:

action<a, b> 

this means function not return , takes 2 arguments, in c# lambda:

(a, b) => { } 

we see 1st argument (a) is: idictionary<string, object>, known owin environment.

the 2nd argument (b) is: func<c, d> means function takes c , returns d. in c# lambda:

(c) => { return d; } 

we need dive 1st argument (c) of 2nd argument (b). , see takes owin environment , returns task.

what accept?

accept tries extract parameters iowincontext , map them websocketaccept type.

if can't extract them null , proceed next middleware.

otherwise websocket request , call function takes 2 parameters (websocketaccept), discussed above (action<a, b>).

the first parameter being ordinary dictionary, contains websocket accept parameters.

the second parameter being function takes dictionary , returns task.

this function called else, code does, pass callback function along caller.

the caller calls function correct argument. because caller knows signature of function. function called after accepting websocket connection request. hence comment callback.

what if wanted pass further down pipeline once have identified web socket request?

well in example, callback function websocketecho pass in function, satisfies function signature of:

task mycallbackfunction(idictionary<string, object> context) {     //     return task.fromresult(0); } 

the takeaway don't call function, function called you. specify after negotiating web socket request connection, decide happens.

the websocketecho function called once every client, , loops until client chooses close connection. meanwhile echo whatever receives.

disclaimer: trying wrap head around web sockets , owin, wanted share findings posterity, since no 1 had answered question. welcome corrections.

edit
noticed own experiment, if return callback function websocketcontext connection aborted. meaning can't send/receive messages on connection if pass websocketcontext around after ending callback.

update
last time tried use on windows 2008 r2 iis 7.5 server not websocket work. according this: https://stackoverflow.com/a/14130152/1640121 - iis 7.5 server not support websockets.
means if application hosted in iis 7.5 not able have websockets.

then thought possible solution:

  1. use separate application, e.g. service program (outside iis) handles websocket requests.
  2. use reverse proxy map request service application

this felt cumbersome me, made me put aside implementing websocket now...


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -