public static WebSocketMessageContext ToContext(this WebSocketReceiveResult result, byte[] input) { if (result == null) { throw new ArgumentNullException(nameof(result)); } if (input == null) { throw new ArgumentNullException(nameof(input)); } var content = Encoding.UTF8.GetString(input, 0, input.Length); WebSocketMessageContext webSocketContext = new WebSocketMessageContext(); try { webSocketContext = JsonConvert.DeserializeObject <WebSocketMessageContext>(content); } catch (Exception ex) { webSocketContext.Command = WebSocketCommands.DataSend; webSocketContext.Value = content; webSocketContext.MessageType = result.MessageType; } webSocketContext.Length = input.Length; return(webSocketContext); }
public async Task BroadcastAsync(WebSocketMessageContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Value == null) { throw new ArgumentNullException(nameof(context.Value)); } if (!Connections.Any()) { return; } var segments = context.ToSegment(); var descriptor = new WebSocketMessageDescriptor { Segments = segments, EndOfMessage = true, MessageType = WebSocketMessageType.Text }; _sendFrameAsyncLock.Wait(); foreach (var connection in Connections) { await SendAsync(connection.Value, descriptor); } _sendFrameAsyncLock.Release(); }
public static async Task <WebSocketMessageContext> ToBinaryContextAsync(this WebSocketReceiveResult result, IStreamCompressor compressor, byte[] input) { if (result == null) { throw new ArgumentNullException(nameof(result)); } var content = input.Split(); byte[] header = content.Item1; byte[] body = content.Item2; var webSocketContext = new WebSocketMessageContext(); bool isCompressed = GZipHelper.IsGZipBody(body); if (isCompressed) { body = await compressor.DeCompressAsync(body); } using (var ms = new MemoryStream(header)) using (var sr = new StreamReader(ms)) { var data = await sr.ReadToEndAsync(); if (data != null) { try { webSocketContext.Header = JsonConvert.DeserializeObject <Dictionary <string, object> >(data); } catch (Exception ex) { webSocketContext.Header = new Dictionary <string, object> { ["Exception"] = ex.Message, ["Unknown"] = "Unknown binary message!" }; } } } using (var ms = new MemoryStream(body)) using (var sr = new StreamReader(ms)) { var data = await sr.ReadToEndAsync(); webSocketContext.Value = data; } webSocketContext.Length = input.Length; webSocketContext.MessageType = WebSocketMessageType.Binary; webSocketContext.Command = WebSocketCommands.DataSend; return(webSocketContext); }
public static MemoryStream ToMemoryStream(this WebSocketMessageContext webSocketContext) { if (webSocketContext == null) { throw new ArgumentNullException(nameof(webSocketContext)); } return(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(webSocketContext)))); }
public static ArraySegment <byte> ToSegment(this WebSocketMessageContext webSocketContext) { if (webSocketContext == null) { throw new ArgumentNullException(nameof(webSocketContext)); } var content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(webSocketContext)); return(new ArraySegment <byte>(content, 0, content.Length)); }
public async Task SendAsync(string connectionId, WebSocketMessageContext context) { if (!Connections.Any()) { return; } _headerProvider.Invoke(context.Header); using (var stream = context.ToMemoryStream()) { await SendDataAsync(stream, WebSocketMessageType.Text, connectionId); } }
public static string GetConnectionId(this WebSocketMessageContext webSocketContext) { if (webSocketContext == null) { throw new ArgumentNullException(nameof(webSocketContext)); } object connectionId = null; if (webSocketContext.Header.TryGetValue(SocketsConstants.ConnectionId, out connectionId)) { return(connectionId.ToString()); } throw new ArgumentOutOfRangeException(nameof(connectionId)); }
public async Task BroadcastBinaryAsync(WebSocketMessageContext context) { if (!Connections.Any()) { return; } using (var ms = context.ToMemoryStream()) { var bytes = await ToBytesAsync(ms.ToArray()); using (var stream = new MemoryStream(bytes)) { await SendDataAsync(stream, WebSocketMessageType.Binary, Connections.Select(c => c.Key).ToArray()); } } }
public async Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "") { var receiverContext = new WebSocketReceiverContext { Compressor = _compressor, ConnectionId = connectionId, LoggerFactory = _loggerFactory, WebSocket = webSocket }; WebSocketTransport transport = null; if (Connections.TryGetValue(connectionId, out transport)) { transport.ReConnect(webSocket); List <MessageHolder> messages = _lifetimeManager.TryDequeue(connectionId); foreach (var message in messages) { await SendAsync(transport, new WebSocketMessageDescriptor { MessageType = WebSocketMessageType.Text, Segments = message.Segments, EndOfMessage = true, IsQueue = true, }); } } else { transport = new WebSocketTransport(webSocket, connectionId, connectorName); var context = new WebSocketMessageContext(); context.Command = WebSocketCommands.Handshake; context.Value = connectionId; context.Header = await _initState.GetStateAsync(); Connections.TryAdd(connectionId, transport); await SendAsync(connectionId, context); } var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, CloseConnection); await receiver.ReceiveAsync(); }
public async Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "", CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken != CancellationToken.None) { cancellationToken.Register(() => { CancellationGraceful(); }); } var receiverContext = new WebSocketReceiverContext { Compressor = _compressor, ConnectionId = connectionId, LoggerFactory = _loggerFactory, WebSocket = webSocket, InvocatorContext = InvocatorContext }; if (Connections.TryGetValue(connectionId, out WebSocketTransport transport)) { transport.ReConnect(webSocket); } else { transport = new WebSocketTransport(webSocket, connectionId, connectorName); var context = new WebSocketMessageContext(); context.Command = WebSocketCommands.Handshake; context.Value = connectionId; context.Header = await _initState.GetStateAsync(); Connections.TryAdd(connectionId, transport); await SendAsync(connectionId, context); } var receiver = new WebSocketReceiver(_serviceProvider, receiverContext, CloseConnection, _loggerFactory); await receiver.ReceiveAsync(cancellationToken); }
public async Task BroadcastAsync(WebSocketMessageContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Value == null) { throw new ArgumentNullException(nameof(context.Value)); } if (!Connections.Any()) { return; } using (var stream = context.ToMemoryStream()) { await SendDataAsync(stream, WebSocketMessageType.Text, Connections.Select(c => c.Key).ToArray()); } }
public Task SendAsync(string connectionId, WebSocketMessageContext context) { if (!Connections.Any()) { return(TaskCache.CompletedTask); } WebSocketTransport transport = null; if (!Connections.TryGetValue(connectionId, out transport)) { throw new ArgumentOutOfRangeException(nameof(transport)); } var segments = context.ToSegment(); var descriptor = new WebSocketMessageDescriptor { Segments = segments, EndOfMessage = true, MessageType = WebSocketMessageType.Text }; return(SendAsync(transport, descriptor)); }