/// <summary> /// Writes a dictionary as a JSON object. /// </summary> public void Write(IDictionary dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } // already implements serialization interface? IJSonWritable jValue = dictionary as IJSonWritable; if (jValue != null) { jValue.Write(this); return; } // is marked with serialization attribute? Type oType = dictionary.GetType(); JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType); if (jsonAttribute != null) { WriteAttributedObject(dictionary, oType, jsonAttribute); return; } WriteObjectBegin(); foreach (DictionaryEntry entry in dictionary) { WriteMember(entry.Key.ToString()); WriteEmbeddedValue(entry.Value); } WriteObjectEnd(); }
/// <summary> /// Init constructor. /// </summary> public HandshakeRequest(BayeuxConnectionTypes supportedConnectionTypes, IJSonWritable data, IJSonWritable ext) : base (MetaChannel, data, ext) { Version = new Version(1, 0); MinimumVersion = Version; SupportedConnectionTypes = supportedConnectionTypes; }
/// <summary> /// Init constructor. /// </summary> public HandshakeRequest(BayeuxConnectionTypes supportedConnectionTypes, IJSonWritable data, IJSonWritable ext) : base(MetaChannel, data, ext) { Version = new Version(1, 0); MinimumVersion = Version; SupportedConnectionTypes = supportedConnectionTypes; }
/// <summary> /// Writes enumerable collection as a JSON array. /// </summary> public void Write(IEnumerable array) { if (array == null) { throw new ArgumentNullException("array"); } // already implements serialization interface? IJSonWritable jValue = array as IJSonWritable; if (jValue != null) { jValue.Write(this); return; } // is marked with serialization attribute? Type oType = array.GetType(); JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType); if (jsonAttribute != null) { WriteAttributedObject(array, oType, jsonAttribute); return; } WriteArrayBegin(); foreach (object value in array) { WriteEmbeddedValue(value); } WriteArrayEnd(); }
/// <summary> /// Init constructor. /// </summary> public SubscribeRequest(string clientID, string subscriptionChannel, IJSonWritable data, IJSonWritable ext) : base(MetaChannel, data, ext) { if (!BayeuxChannel.IsValid(subscriptionChannel)) throw new ArgumentException("SubscriptionChannel failed a standard validation", "subscriptionChannel"); ClientID = clientID; SubscriptionChannel = subscriptionChannel; }
/// <summary> /// Init constructor. /// </summary> public PublishRequest(string clientID, string channel, IJSonWritable eventData, IJSonWritable data, IJSonWritable ext) : base (channel, data, ext) { if (!BayeuxChannel.IsValid(channel)) throw new ArgumentException("Channel failed a standard validation", "channel"); ClientID = clientID; EventData = eventData; }
/// <summary> /// Init constructor. /// </summary> public ConnectRequest(string clientID, BayeuxConnectionTypes connectionType, IJSonWritable data, IJSonWritable ext) : base (MetaChannel, data, ext) { if (string.IsNullOrEmpty(clientID)) throw new ArgumentException("ClientID can not be empty", "clientID"); ClientID = clientID; ConnectionType = connectionType; }
/// <summary> /// Writes a serializable object as JSON string. /// </summary> public void Write(IJSonWritable o) { if (o == null) { throw new ArgumentNullException("o"); } o.Write(this); }
/// <summary> /// Sends publish request to the server. /// </summary> public void Publish(IJSonWritable data, IJSonWritable ext, string channel, IJSonWritable eventData, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) { throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); } SendRequest(new PublishRequest(ClientID, channel, eventData, data, ext), asynchronous); }
/// <summary> /// Sends connect request to the server. /// </summary> public void Connect(IJSonWritable data, IJSonWritable ext, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) { throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); } SendRequest(new ConnectRequest(ClientID, BayeuxConnectionTypes.LongPolling, data, ext), asynchronous); }
/// <summary> /// Init constructor. /// </summary> public ConnectRequest(string clientID, BayeuxConnectionTypes connectionType, IJSonWritable data, IJSonWritable ext) : base(MetaChannel, data, ext) { if (string.IsNullOrEmpty(clientID)) { throw new ArgumentException("ClientID can not be empty", "clientID"); } ClientID = clientID; ConnectionType = connectionType; }
/// <summary> /// Init constructor. /// </summary> public PublishRequest(string clientID, string channel, IJSonWritable eventData, IJSonWritable data, IJSonWritable ext) : base(channel, data, ext) { if (!BayeuxChannel.IsValid(channel)) { throw new ArgumentException("Channel failed a standard validation", "channel"); } ClientID = clientID; EventData = eventData; }
/// <summary> /// Default constructor. /// </summary> public BayeuxRequest(string channel, IJSonWritable data, IJSonWritable ext) { FormattedOutput = true; RequestMethod = HttpDataSource.MethodPost; Channel = channel; _id++; ID = _id.ToString(CultureInfo.InvariantCulture); _data = data; _ext = ext; }
/// <summary> /// Init constructor. /// </summary> public SubscribeRequest(string clientID, string subscriptionChannel, IJSonWritable data, IJSonWritable ext) : base(MetaChannel, data, ext) { if (!BayeuxChannel.IsValid(subscriptionChannel)) { throw new ArgumentException("SubscriptionChannel failed a standard validation", "subscriptionChannel"); } ClientID = clientID; SubscriptionChannel = subscriptionChannel; }
/// <summary> /// Sends unsubscription request to the server. /// </summary> public void Unsubscribe(IJSonWritable data, IJSonWritable ext, string channel, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) { throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); } if (!Subscribed(channel)) { throw new BayeuxException(string.Format(CultureInfo.InvariantCulture, "Not subscribed to channel: '{0}'", channel)); } SendRequest(new UnsubscribeRequest(ClientID, channel, data, ext), asynchronous); }
/// <summary> /// Sends handshake request to the server. /// </summary> public void Handshake(BayeuxConnectionTypes supportedConnectionTypes, IJSonWritable data, IJSonWritable ext, bool asynchronous) { // if there is another Handshake request executed in background, try to cancel it first: if (_state == BayeuxConnectionState.Connecting) { Cancel(); // <-- this should reset the state to Disconnected! } if (_state != BayeuxConnectionState.Disconnected) { throw new InvalidOperationException("Connecting or already connected to bayeux server! Disconnect first"); } _state = BayeuxConnectionState.Connecting; SendRequest(new HandshakeRequest(supportedConnectionTypes, data, ext), asynchronous); }
/// <summary> /// Writes whole object represented as dictionary or enumerable collection. /// </summary> public void Write(object o) { // try to access object as IJSonWritable class: IJSonWritable jValue = o as IJSonWritable; if (jValue != null) { Write(jValue); return; } if (o != null) { Type oType = o.GetType(); JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType); if (jsonAttribute != null) { WriteAttributedObject(o, oType, jsonAttribute); return; } } // try to access object as dictionary: IDictionary dict = o as IDictionary; if (dict != null) { Write(dict); return; } // try to access object as enumerable: IEnumerable array = o as IEnumerable; if (array != null) { Write(array); return; } throw new ArgumentException("Invalid object type to serialize to JSON", "o"); }
/// <summary> /// Sends handshake request to the server. /// </summary> public void Handshake(BayeuxConnectionTypes supportedConnectionTypes, IJSonWritable data, IJSonWritable ext, bool asynchronous) { // if there is another Handshake request executed in background, try to cancel it first: if (_state == BayeuxConnectionState.Connecting) Cancel(); // <-- this should reset the state to Disconnected! if (_state != BayeuxConnectionState.Disconnected) throw new InvalidOperationException("Connecting or already connected to bayeux server! Disconnect first"); _state = BayeuxConnectionState.Connecting; SendRequest(new HandshakeRequest(supportedConnectionTypes, data, ext), asynchronous); }
public void Write(IJSonWritable o) { _output.Write(o); }
/// <summary> /// Init constructor. /// </summary> public HandshakeRequest(IJSonWritable data, IJSonWritable extData) : this(BayeuxConnectionTypes.LongPolling | BayeuxConnectionTypes.CallbackPolling | BayeuxConnectionTypes.Iframe, data, extData) { }
/// <summary> /// Sends handshake request to the server. /// </summary> public void Handshake(BayeuxConnectionTypes supportedConnectionTypes, IJSonWritable data, string userName, string password, bool asynchronous) { Handshake(supportedConnectionTypes, data, new BayeuxHandshakeExtension(userName, password), asynchronous); }
/// <summary> /// Sends subscription request to the server. /// </summary> public void Publish(string channel, IJSonWritable eventData) { Publish(null, null, channel, eventData, true); }
/// <summary> /// Sends unsubscription request to the server. /// </summary> public void Unsubscribe(IJSonWritable data, IJSonWritable ext, string channel, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); if (!Subscribed(channel)) throw new BayeuxException(string.Format(CultureInfo.InvariantCulture, "Not subscribed to channel: '{0}'", channel)); SendRequest(new UnsubscribeRequest(ClientID, channel, data, ext), asynchronous); }
public void Write(IJSonWritable o) { throw new NotImplementedException(); }
/// <summary> /// Init constructor. /// </summary> public DisconnectRequest(string clientID, IJSonWritable data, IJSonWritable ext) : base(MetaChannel, data, ext) { ClientID = clientID; }
private void WriteEmbeddedValue(object oValue) { // check if this is a struct: if (oValue is Single) { WriteValue((Single)oValue); return; } if (oValue is Double) { WriteValue((Double)oValue); return; } if (oValue is Decimal) { WriteValue((Decimal)oValue); return; } if (oValue is DateTime) { WriteValue((DateTime)oValue); return; } if (oValue is TimeSpan) { WriteValue((TimeSpan)oValue); return; } if (oValue is Boolean) { WriteValue((Boolean)oValue); return; } // check if this is a class object: if (oValue == null) { WriteValueNull(); return; } if (oValue is DBNull) { WriteValueNull(); return; } if (oValue is String || oValue is Char) { WriteValue(oValue.ToString()); return; } if (oValue is Guid) { WriteValue((Guid)oValue); return; } if (oValue is Enum) { WriteValue(oValue.ToString()); return; } // does it implement the dedicated serialization interface? IJSonWritable jValue = oValue as IJSonWritable; if (jValue != null) { jValue.Write(this); return; } // is it marked with serialization attribute? Type oType = oValue.GetType(); JSonSerializableAttribute jsonAttribute = ReflectionHelper.GetCustomAttribute <JSonSerializableAttribute>(oType); if (jsonAttribute != null) { WriteAttributedObject(oValue, oType, jsonAttribute); return; } // is a dictionary? IDictionary dict = oValue as IDictionary; if (dict != null) { Write(dict); return; } // is an array of elements? IEnumerable array = oValue as IEnumerable; if (array != null) { Write(array); } else { WriteValueInternal(oValue.ToString(), false); } }
/// <summary> /// Sends disconnect request to the server. /// </summary> public void Disconnect(IJSonWritable data, IJSonWritable ext, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); SendRequest(new DisconnectRequest(ClientID, data, ext), asynchronous); }
/// <summary> /// Sends publish request to the server. /// </summary> public void Publish(IJSonWritable data, IJSonWritable ext, string channel, IJSonWritable eventData, bool asynchronous) { if (_state != BayeuxConnectionState.Connected) throw new InvalidOperationException("Not connected to the server - handshake must be performed first"); SendRequest(new PublishRequest(ClientID, channel, eventData, data, ext), asynchronous); }
/// <summary> /// Writes a serializable object as JSON string. /// </summary> public void Write(IJSonWritable o) { if (o == null) throw new ArgumentNullException("o"); o.Write(this); }
/// <summary> /// Sends subscription request to the server in a synchronous way. /// Useful only when testing as blocks the current thread. /// </summary> public void PublishSync(string channel, IJSonWritable eventData) { Publish(null, null, channel, eventData, false); }
/// <summary> /// Init constructor. /// </summary> public DisconnectRequest(string clientID, IJSonWritable data, IJSonWritable ext) : base (MetaChannel, data, ext) { ClientID = clientID; }