Example #1
0
        private Task SendJson(WebSocker socket, Message message, CancellationToken token = default(CancellationToken))
        {
            var serializedMessage = JsonConvert.SerializeObject(message);
            var buffer            = new ArraySegment <byte>(array: Encoding.UTF8.GetBytes(serializedMessage),
                                                            offset: 0,
                                                            count: serializedMessage.Length);

            return(socket.SendAsync(buffer, WebSocketMessageType.Text, cancellationToken: token, endOfMessage: true));
        }
Example #2
0
 public virtual Task OnConnected(WebSocker socket)
 {
     ConnectionManager.AddSocket(socket);
     return(SendMessage(socket, new MessageResponse
     {
         MessageType = MessageType.ConnectionEvent,
         Data = new MessageData {
             ["SocketId"] = socket.Id
         }
     }));
 }
Example #3
0
 public Task SendMessage(WebSocker socket, MessageResponse message,
                         CancellationToken token = default(CancellationToken))
 {
     // TODO: maybe throw an error?
     if (socket.State != WebSocketState.Open)
     {
         return(Task.CompletedTask);
     }
     message.Status = message.Status ?? 200;
     message.Data   = message.Data ?? new MessageData();
     return(SendJson(socket, message, token));
 }
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                if (_next != null)
                {
                    await _next(context);
                }
                return;
            }

            var selectedProtocol = GetProtocol(
                context.Request.Headers["Sec-WebSocket-Protocol"]);

            var socket = await(selectedProtocol == null
                                ? context.WebSockets.AcceptWebSocketAsync()
                                : context.WebSockets.AcceptWebSocketAsync(selectedProtocol));

            var socker = new WebSocker(socket, context);

            await Handler.OnConnected(socker);

            await Receive(socker, async (result, message) =>
            {
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    await Handler.OnDisconnected(socker);
                    return;
                }

                var proto         = selectedProtocol ?? Protocols[0];
                var passedMessage = new HandlerMessage
                {
                    Socket   = socker, SocketResult = result, Serialized = message, Subprotocol = proto,
                    BasePath = context.Request.Path, HttpContext = context
                };
                await Handler.Route(new Request {
                    Context = passedMessage
                });
            });
        }
 public Task RemoveSocket(WebSocker socket) =>
 RemoveSocket(socket.Id);
 public void AddSocket(WebSocker socket)
 {
     Sockets.TryAdd(socket.Id, socket);
 }
Example #7
0
 public virtual Task OnDisconnected(WebSocker socket) =>
 ConnectionManager.RemoveSocket(socket.Id);
Example #8
0
 protected Task SendMessage(WebSocker socket, MessageResponse message, CancellationToken token) =>
 Handler.SendMessage(socket, message, token);