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; }
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)); }); }
public static Task Timeout(this IReceivingConnection connection) { var command = new SignalCommand { Type = CommandType.Timeout }; return connection.SendCommand(command); }
public static Task Close(this IReceivingConnection connection) { var command = new SignalCommand { Type = CommandType.Disconnect, ExpiresAfter = TimeSpan.FromMinutes(30) }; return connection.SendCommand(command); }
/// <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)); }
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(); }
/// <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) }); }
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)); }
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); } } } }
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); }
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); }
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; } }
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; } } }
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; } }
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); }
public Task SendCommand(SignalCommand command) { return SendMessage(SignalCommand.AddCommandSuffix(_connectionId), command); }
public Task SendCommand(SignalCommand command) { return(SendMessage(SignalCommand.AddCommandSuffix(_connectionId), command)); }
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; } }
public Task SendCommand(SignalCommand command) { return(SendMessage(_connectionId + "." + SignalCommand.SignalrCommand, command)); }