c# - Websocket server on Azure -
i'm trying host websockets server on azure. i'm bit confused , hoping me.
i've followed many articles code close 1 article : https://azure.microsoft.com/en-us/blog/introduction-to-websockets-on-windows-azure-web-sites/
public void processrequest(httpcontext context) { if (context.iswebsocketrequest) { context.acceptwebsocketrequest(processws); } } public bool isreusable { { return false; } } private async task processws(aspnetwebsocketcontext context) { try { websocket socket = context.websocket; while (true) { var url = context.requesturi; arraysegment<byte> buffer = new arraysegment<byte>(new byte[1024]); websocketreceiveresult result = await socket.receiveasync(buffer, cancellationtoken.none); if (socket.state == websocketstate.open) { string usermessage = encoding.utf8.getstring(buffer.array, 0, result.count) .trim(new char[] { ' ' }); // remove spaces before , after // } else { break; } } } catch (exception e) { log.info("exception" + e.message + " >>>" + e.stacktrace); } }
this works fine, i'm able messages devices , answer them.
but in cases need send message device, example :
device sends "tell device b blink"
since it's websockets server , device b has talked server should have somewhere connection opened device b. , when device asks me can send message device b.
but how can achieve code ? how can find connection device b ? if not possible how should ?
i hope problem described enough understood.
thank you,
but how can achieve code ? how can find connection device b ? if not possible how should ?
according scenario, followed tutorial webapi-and-websockets , implement my web api project establish connection between clients. here screenshoot test, refer it:
additionally, leverage signalr , map client (user) signalr connections. more details, refer mapping signalr users connections. also, refer git sample microsoft.aspnet.signalr.samples.
update:
how use signalr in case ?
for simple way, retain connection , user info stored in memory, more details, refer in-memory storage. based on scenario, wrote sample project aspdotnet-signalr-chat, refer it, , here screenshot test:
Comments
Post a Comment