示例#1
0
        /// <summary>
        /// Отправка сообщения клиенту
        /// </summary>
        /// <param name="socket">сокет</param>
        /// <param name="packet">пакет данных</param>
        /// <param name="ct">токен прерывания задачи</param>
        /// <returns></returns>
        private static Task SendPacketAsync(WebSocket socket, Models.PacketModel packet, CancellationToken ct = default(CancellationToken))
        {
            var buffer  = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(packet));
            var segment = new ArraySegment <byte>(buffer);

            return(socket.SendAsync(segment, WebSocketMessageType.Text, true, ct));
        }
示例#2
0
        /// <summary>
        /// Получение пакета от клиента
        /// </summary>
        /// <param name="socket">сокет</param>
        /// <param name="ct">токен прерывания выполнения задачи</param>
        /// <returns></returns>
        private static async Task <Models.PacketModel> ReceivePacketAsync(WebSocket socket, CancellationToken ct = default(CancellationToken))
        {
            var buffer = new ArraySegment <byte>(new byte[8192]);

            using (var ms = new MemoryStream())
            {
                WebSocketReceiveResult result;
                do
                {
                    ct.ThrowIfCancellationRequested();

                    result = await socket.ReceiveAsync(buffer, ct);

                    ms.Write(buffer.Array, buffer.Offset, result.Count);
                }while (!result.EndOfMessage);

                ms.Seek(0, SeekOrigin.Begin);
                if (result.MessageType != WebSocketMessageType.Text)
                {
                    return(null);
                }

                string             jsonStr = Encoding.UTF8.GetString(buffer);
                Models.PacketModel pack    = JsonConvert.DeserializeObject <Models.PacketModel>(jsonStr);
                //от клиента можно получить только сообщение
                switch (pack.packetType)
                {
                case Models.PacketType.Message:
                    return(JsonConvert.DeserializeObject <Models.MessageModel>(jsonStr));

                default:
                    return(null);
                }
            }
        }
示例#3
0
        private async void AnalizePacket(Models.PacketModel packet, CancellationToken ct = default(CancellationToken))
        {
            switch (packet.packetType)
            {
            case Models.PacketType.Message:
                Models.MessageModel packetMessage = packet as Models.MessageModel;
                if (string.IsNullOrEmpty(packetMessage.dst_user))
                {
                    //отправка сообщений всем клиентам
                    foreach (var socket in m_sockets)
                    {
                        if (socket.Value.State != WebSocketState.Open)
                        {
                            continue;
                        }
                        await SendPacketAsync(socket.Value, packetMessage, ct);
                    }
                }
                else
                {
                    WebSocket socket;
                    if (m_sockets.TryGetValue(packetMessage.dst_user, out socket))
                    {
                        await SendPacketAsync(socket, packet, ct);
                    }
                    if (packetMessage.src_user != packetMessage.dst_user &&
                        m_sockets.TryGetValue(packetMessage.src_user, out socket))
                    {
                        await SendPacketAsync(socket, packet, ct);
                    }
                }
                break;

            default:
                return;
            }
        }