public void SendMessage(PendingDownstreamMessage message) { _client.Touch(); if (_downstreamId.HasValue) { var sb = new StringBuilder(); sb.Append("<script>parent.__mr('"); sb.Append(_client.Key); sb.Append("',"); sb.Append(_downstreamId.Value); sb.Append(","); BuildResponseJson(sb, message); sb.AppendLine(");</script>"); Context.Response.Write(sb.ToString()); Context.Response.Flush(); } else { var sb = new StringBuilder(); BuildResponseJson(sb, message); string response = sb.ToString(); Context.Response.Write(String.Format("{0}\n", response.Length)); Context.Response.Write(response); Context.Response.Flush(); } }
private void BuildResponseJson(StringBuilder sb, PendingDownstreamMessage message) { using (var stringWriter = new StringWriter(sb)) using (var writer = new JsonTextWriter(stringWriter)) { if (message == null) { writer.WriteStartArray(); writer.WriteValue("noop"); writer.WriteEndArray(); } else { var responseType = message.Message.GetType(); var responseServiceType = ServiceRegistry.GetAssembly(responseType.Assembly).TypesByType[responseType]; writer.WriteStartObject(); writer.WritePropertyName("r"); writer.WriteValue((int)message.Kind); writer.WritePropertyName("a"); writer.WriteValue((int)message.AssociationId); writer.WritePropertyName("t"); writer.WriteValue(responseServiceType.Message.Id); writer.WritePropertyName("p"); JsonUtil.SerializeMessage(writer, responseServiceType, message.Message); writer.WriteEndObject(); } } }