c# - How to keep track of established connections using WebSockets -


I am trying to get real-time chat service in life for cross-platform devices. The problem is that the System.Net.WebSockets namespace does not allow me to keep track of the connection established directly. I can take a session ID of the current connection, but how can I tell that the following work try socket. SendAsync (buffer, WebSocketMessageType.Text, cancellation token.one) for specific clients < / P>

If I could use the Microsoft.WebSockets , then I am likely to create a WebSocketCollection () and Do something like the client. The message (related to the message) belongs to , but I through it a ArraySegment & lt; Byte [] & gt; I can not send it also that it is more for AJAX customers and websites and this stuff.

I now have the following code snippet:

  Public class WSHandler: IHttpHandler {event NewConnectionEventHandler NewConnection; Public Zero Processing Request (HTTP Contact Reference) {if (context.IsWebSocketRequest) {reference.AcceptWebSocketRequest (ProcessWSChat); }} Public Child IRUUB ({Return False}}} WSChat (AspNetWebSocketContext Reference) as a Private Async Work Process {WebSocket Socket = Reference WebSocket; Int myHash = socket.GetHashCode (); while (true) {ArraySegment & lt; Byte & gt; buffer = new array split & lt; byte & gt; (new byte [1024]); WebSocketReceiveResult result = awaiting socket; ResaveSync (buffer, cancellation token.); If (socket.state == WebSocketState. Open) {string userMessage = Encoding.ASCII.GetString (buffer.he, 0, result. Count); UserMessage = "You have sent:" + Usage Karta message + "at" + date time.Nob toLongTimeString () + "from ip" + context.UserHostAddress.ToString (); buffer = new array partition & lt; byte & gt; (encoding.assici.getbites (user message) );) Wait for socket: Send accec (buffer, website message type, text, true, cancellation token one);} and {brake;}}}}   

I How to extend the project, save session / connection and send a message to a specific user connection Can i call

Normally, you do not do anything like WebSketches for just another layer or sub-system and End point should be for example, on an event-driven architecture connection, you should start the handler, collect data from the attached socket like cookie, url or whatever, and some should inform that with that user That cookie / url is connected to that socket.

Your code:

  • Do not declare buffer reader inside the loop, you can reuse it.
  • Websites always send text data as UTF 8, not ASCII.

    This will be a way for your users to track in a small project, but I recommend to webcast your web site to another message structure such as Mastrancit: Public class WSHandler: IHttpHandler {public bool IRUUB (Received {return false;}} Public vacuum process request (HttpContext reference) {if (context.IsWebSocketRequest) {context.AcceptWebSocketRequest (ProcessWSChat);}} Static reading ConcurrentDictionary for & lt; IPrincipal, WebSocket & gt; _use rs = New ConcurrentDictionary & lt; IPrincipal, WebSocket & gt; (); Private async work process WSChat (AspNetWebSocketContext reference) {WebSocket socket = context.website; ArraySegment & lt; byte & gt; buffer = new array division & lt; byte & gt; (new byte [4096]); // user by cookie or whatever and specify a user object verb cookie = cookies ["mikuki"]; var url = context.RequestUri; IPrincipal myUser = GetUser (cookie, url); // Uses user come from ASP.NET authentication. myUser = context.User; _users.AddOrUpdate (myUser, Socket, (P, W) => Socket); While (socket.state == WebSocketStat.Open) {WebSite Result Results Result = Waiting for Socket ResaveSync (buffer, cancellation token one). Configure (wrong); String user message = encoding. UTF8.GetString (buffer.he, 0, result. Count); userMessage = "You have sent:" + User message + "at" + date time.Now ToLongTimeString () + "From ip" + context.UserHostAddress.ToString (); Var sent buffer = new array split & lt; Byte & gt; (Encoding. UTF8.GetBytes (user message)); Wait for the socket SendAsync (sendbuffer, WebSocketMessageType.Text, true, cancellation token. A). ConfigureHouse (wrong); } // When the connection is terminated, try to remove the websetkete from the user; If (_users.TryRemove (myUser, out ows)) {if (ows! = Socket) {// whops! The user quickly connects again and if you are removing new connections //, put it back _users. addOrUpdate (myUser, ows, (p, w) => ows); }}} Private IPrincipal GetUser (HTTP Cookie Cookie, URI URL) {New Non Imulated Exposition (throw); }}

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -