public bool Add(ConnectionPrincipal tenant, String secret) { var pair = new Pair<ConnectionPrincipal, String>(tenant, secret); if (_tenantsDictionary.TryAdd(tenant.Identity.Name, pair)) { tenant.Disposed += (sender, args) => { IPrincipal principal = sender as IPrincipal; Pair<ConnectionPrincipal, string> ignore; if (principal != null) _tenantsDictionary.TryRemove(principal.Identity.Name, out ignore); }; return true; } return false; }
public VtortWebsocket(WebSocket webSocket, ConnectionPrincipal principal) { _webSocket = webSocket; _principal = principal; Task.Factory.StartNew(WriteMessageAsync); }
public void OnOpen() { Trace.TraceInformation("WCFWebsocket is Opened"); _principal = Thread.CurrentPrincipal as ConnectionPrincipal; if (_principal == null) { throw new Exception("Unauthenticated"); } _principal.OperationMessageListener.OnConnect(this); }
private async Task HandleWebSocketAsync(WebSocket websocket, ConnectionPrincipal connectionPrincipal) { VtortWebsocket soWebsocket = new VtortWebsocket(websocket, connectionPrincipal); try { Trace.TraceInformation("Server onConnect()"); connectionPrincipal.OperationMessageListener.OnConnect(soWebsocket); while (websocket.IsConnected) { var message = await websocket.ReadMessageAsync(CancellationToken.None) .ConfigureAwait(false); if (message != null) { switch (message.MessageType) { case WebSocketMessageType.Text: using (var sr = new StreamReader(message, Encoding.UTF8)) { String msgContent = await sr.ReadToEndAsync(); connectionPrincipal.OperationMessageListener.OnMessage(soWebsocket, msgContent); } break; case WebSocketMessageType.Binary: using (var ms = new MemoryStream()) { await message.CopyToAsync(ms); if (ms.Length > 0) { connectionPrincipal.OperationMessageListener.OnMessage(soWebsocket, ms.ToArray()); } } break; } } } } catch (Exception ex) { connectionPrincipal.OperationMessageListener.OnError(ex); } finally { connectionPrincipal.OperationMessageListener.OnClose(soWebsocket, 1000, ""); //Dispose before onClose is complete ??? websocket.Dispose(); } }