예제 #1
0
        internal static bool TryGetCommand(Message message, IJsonSerializer serializer, out SignalCommand command)
        {
            command = null;
            if (!message.SignalKey.EndsWith(SignalrCommand, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            command = message.Value as SignalCommand;

            // Optimization for in memory message store
            if (command != null)
            {
                return true;
            }

            // Otherwise deserialize the message value
            string rawValue = message.Value as string;
            if (rawValue == null)
            {
                return false;
            }

            command = serializer.Parse<SignalCommand>(rawValue);
            return true;
        }
예제 #2
0
        public Message(string signalKey, long id, object value, DateTime created)
        {
            SignalKey = signalKey;
            Value     = value;
            Id        = id;
            Created   = created;

            _command = new Lazy <SignalCommand>(() =>
            {
                if (!SignalCommand.IsCommand(this))
                {
                    return(null);
                }

                var command = Value as SignalCommand;

                // Optimization for in memory message store
                if (command != null)
                {
                    return(command);
                }

                // Otherwise deserialize the message value
                string rawValue = Value as string;
                if (rawValue == null)
                {
                    return(null);
                }

                return(DependencyResolver.Resolve <IJsonSerializer>().Parse <SignalCommand>(rawValue));
            });
        }
예제 #3
0
        public static Task Timeout(this IReceivingConnection connection)
        {
            var command = new SignalCommand
            {
                Type = CommandType.Timeout
            };

            return connection.SendCommand(command);
        }
예제 #4
0
        public static Task Close(this IReceivingConnection connection)
        {
            var command = new SignalCommand
            {
                Type = CommandType.Disconnect,
                ExpiresAfter = TimeSpan.FromMinutes(30)
            };

            return connection.SendCommand(command);
        }
예제 #5
0
        /// <summary>
        /// Removes a connection from the specified group.
        /// </summary>
        /// <param name="connectionId">The connection id to remove from the group.</param>
        /// <param name="groupName">The name of the group</param>
        /// <returns>A task that represents the connection id being removed from the group.</returns>
        public Task Remove(string connectionId, string groupName)
        {
            var command = new SignalCommand
            {
                Type  = CommandType.RemoveFromGroup,
                Value = CreateQualifiedName(groupName)
            };

            return(_connection.Send(SignalCommand.AddCommandSuffix(connectionId), command));
        }
        private Task SendCommand(string clientId, CommandType type, object value)
        {
            string signal = clientId + "." + SignalrCommand;

            var groupCommand = new SignalCommand
            {
                Type  = type,
                Value = value
            };

            return(Connection.Broadcast(signal, groupCommand));
        }
예제 #7
0
        private void SerializePesistentResponse(PersistentResponse response, TextWriter writer)
        {
            var jsonWriter = new JsonTextWriter(writer);

            jsonWriter.WriteStartObject();

            jsonWriter.WritePropertyName("MessageId");
            jsonWriter.WriteValue(response.MessageId);

            jsonWriter.WritePropertyName("Disconnect");
            jsonWriter.WriteValue(response.Disconnect);

            jsonWriter.WritePropertyName("TimedOut");
            jsonWriter.WriteValue(response.TimedOut);

            if (response.TransportData != null)
            {
                jsonWriter.WritePropertyName("TransportData");
                jsonWriter.WriteStartObject();

                object value;
                if (response.TransportData.TryGetValue("Groups", out value))
                {
                    jsonWriter.WritePropertyName("Groups");
                    jsonWriter.WriteStartArray();
                    foreach (var group in (IEnumerable <string>)value)
                    {
                        jsonWriter.WriteValue(group);
                    }
                    jsonWriter.WriteEndArray();
                }

                jsonWriter.WriteEndObject();
            }

            jsonWriter.WritePropertyName("Messages");
            jsonWriter.WriteStartArray();

            for (int i = 0; i < response.Messages.Count; i++)
            {
                for (int j = response.Messages[i].Offset; j < response.Messages[i].Offset + response.Messages[i].Count; j++)
                {
                    Message message = response.Messages[i].Array[j];
                    if (!SignalCommand.IsCommand(message))
                    {
                        jsonWriter.WriteRawValue(message.Value);
                    }
                }
            }

            jsonWriter.WriteEndArray();
            jsonWriter.WriteEndObject();
        }
예제 #8
0
        /// <summary>
        /// Returns the default signals for the <see cref="PersistentConnection"/>.
        /// </summary>
        /// <param name="connectionId">The id of the incoming connection.</param>
        /// <returns>The default signals for this <see cref="PersistentConnection"/>.</returns>
        protected IEnumerable <string> GetDefaultSignals(string connectionId)
        {
            // The list of default signals this connection cares about:
            // 1. The default signal (the type name)
            // 2. The connection id (so we can message this particular connection)
            // 3. Connection id + SIGNALRCOMMAND -> for built in commands that we need to process

            return(new string[] {
                DefaultSignal,
                connectionId,
                SignalCommand.AddCommandSuffix(connectionId)
            });
        }
예제 #9
0
        protected virtual IConnection CreateConnection(string connectionId, IEnumerable <string> groups, HttpContextBase context)
        {
            // The list of default signals this connection cares about:
            // 1. The default signal (the type name)
            // 2. The connection id (so we can message this particular connection)
            // 3. connection id + SIGNALRCOMMAND -> for built in commands that we need to process
            var signals = new string[] {
                DefaultSignal,
                connectionId,
                SignalCommand.AddCommandSuffix(connectionId)
            };

            return(new Connection(_store, _jsonSerializer, _signaler, DefaultSignal, connectionId, signals, groups));
        }
예제 #10
0
 private void ProcessResults(MessageResult result)
 {
     for (int i = 0; i < result.Messages.Count; i++)
     {
         for (int j = result.Messages[i].Offset; j < result.Messages[i].Offset + result.Messages[i].Count; j++)
         {
             Message message = result.Messages[i].Array[j];
             if (SignalCommand.IsCommand(message))
             {
                 var command = _serializer.Parse <SignalCommand>(message.Value);
                 ProcessCommand(command);
             }
         }
     }
 }
예제 #11
0
파일: Connection.cs 프로젝트: thevu/SignalR
        private List <object> ProcessResults(List <Message> source)
        {
            var messageValues = new List <object>();

            foreach (var message in source)
            {
                if (SignalCommand.IsCommand(message))
                {
                    ProcessCommand(message);
                }
                else
                {
                    messageValues.Add(message.Value);
                }
            }
            return(messageValues);
        }
예제 #12
0
        private List <object> ProcessResults(IList <Message> source)
        {
            var messageValues = new List <object>();

            foreach (var message in source)
            {
                if (SignalCommand.IsCommand(message))
                {
                    var command = WrappedValue.Unwrap <SignalCommand>(message.Value, _serializer);
                    ProcessCommand(command);
                }
                else
                {
                    messageValues.Add(WrappedValue.Unwrap(message.Value, _serializer));
                }
            }
            return(messageValues);
        }
예제 #13
0
        private void ProcessCommand(SignalCommand command)
        {
            switch (command.Type)
            {
            case CommandType.AddToGroup:
                _groups.Add((string)command.Value);
                break;

            case CommandType.RemoveFromGroup:
                _groups.Remove((string)command.Value);
                break;

            case CommandType.Disconnect:
                _disconnected = true;
                break;

            case CommandType.Abort:
                _aborted = true;
                break;
            }
        }
예제 #14
0
 public void ProcessCommand(SignalCommand command)
 {
     lock (Signals)
     {
         switch (command.Type)
         {
             case CommandType.AddToGroup:
                 _groups.Add((string)command.Value);
                 break;
             case CommandType.RemoveFromGroup:
                 _groups.Remove((string)command.Value);
                 break;
             case CommandType.Disconnect:
                 _disconnected = true;
                 break;
             case CommandType.Abort:
                 _aborted = true;
                 break;
         }
     }
 }
예제 #15
0
        private void ProcessCommand(SignalCommand command)
        {
            switch (command.Type)
            {
            case CommandType.AddToGroup:
            {
                var groupData = _serializer.Parse <GroupData>(command.Value);

                if (EventAdded != null)
                {
                    _groups.Add(groupData.Name);
                    EventAdded(groupData.Name, groupData.Cursor);
                }
            }
            break;

            case CommandType.RemoveFromGroup:
            {
                var groupData = _serializer.Parse <GroupData>(command.Value);

                if (EventRemoved != null)
                {
                    _groups.Remove(groupData.Name);
                    EventRemoved(groupData.Name);
                }
            }
            break;

            case CommandType.Disconnect:
                _disconnected = true;
                break;

            case CommandType.Abort:
                _aborted = true;
                break;
            }
        }
예제 #16
0
        private Task SendCommand(string clientId, CommandType type, object value)
        {
            string signal = clientId + "." + SignalrCommand;

            var groupCommand = new SignalCommand
            {
                Type = type,
                Value = value
            };

            return Connection.Broadcast(signal, groupCommand);
        }
예제 #17
0
 public Task SendCommand(SignalCommand command)
 {
     return SendMessage(SignalCommand.AddCommandSuffix(_connectionId), command);
 }
예제 #18
0
 public Task SendCommand(SignalCommand command)
 {
     return(SendMessage(SignalCommand.AddCommandSuffix(_connectionId), command));
 }
예제 #19
0
        private void ProcessCommand(SignalCommand command)
        {
            switch (command.Type)
            {
                case CommandType.AddToGroup:
                    {
                        var groupData = _serializer.Parse<GroupData>(command.Value);

                        if (EventAdded != null)
                        {
                            EventAdded(groupData.Name, groupData.Cursor);
                        }
                    }
                    break;
                case CommandType.RemoveFromGroup:
                    {
                        var groupData = _serializer.Parse<GroupData>(command.Value);

                        if (EventRemoved != null)
                        {
                            EventRemoved(groupData.Name);
                        }
                    }
                    break;
                case CommandType.Disconnect:
                    _disconnected = true;
                    break;
                case CommandType.Abort:
                    _aborted = true;
                    break;
            }
        }
예제 #20
0
 public Task SendCommand(SignalCommand command)
 {
     return(SendMessage(_connectionId + "." + SignalCommand.SignalrCommand, command));
 }