Example #1
0
        void IHub.Call(ClientMessage msg)
        {
            IHub thisHub = this as IHub;
            lock (thisHub.Connection.SyncRoot)
            {
                SentMessages.Add(msg.CallIdx, msg);

                thisHub.Connection.SendJson(BuildMessage(msg));
            }
        }
Example #2
0
        /// <summary>
        /// Builds a JSon string from the given message.
        /// </summary>
        private string BuildMessage(ClientMessage msg)
        {
            try
            {
                builder.Append("{\"H\":\"");
                builder.Append(this.Name);
                builder.Append("\",\"M\":\"");
                builder.Append(msg.Method);
                builder.Append("\",\"A\":");

                string jsonEncoded = string.Empty;

                // Arguments
                if (msg.Args != null && msg.Args.Length > 0)
                    jsonEncoded = (this as IHub).Connection.JsonEncoder.Encode(msg.Args);
                else
                    jsonEncoded = "[]";

                builder.Append(jsonEncoded);

                builder.Append(",\"I\":\"");
                builder.Append(msg.CallIdx.ToString());
                builder.Append("\"");

                // State, if any
                if (msg.Hub.state != null && msg.Hub.state.Count > 0)
                {
                    builder.Append(",\"S\":");

                    jsonEncoded = (this as IHub).Connection.JsonEncoder.Encode(msg.Hub.state);
                    builder.Append(jsonEncoded);
                }

                builder.Append("}");

                return builder.ToString();
            }
            catch (Exception ex)
            {
                HTTPManager.Logger.Exception("Hub - " + this.Name, "Send", ex);

                return null;
            }
            finally
            {
                // reset the StringBuilder instance, to reuse next time
                builder.Length = 0;
            }
        }
        bool IHub.Call(ClientMessage msg)
        {
            IHub thisHub = this as IHub;

            lock (thisHub.Connection.SyncRoot)
            {
                if (!thisHub.Connection.SendJson(BuildMessage(msg)))
                    return false;
                
                SentMessages.Add(msg.CallIdx, msg);
            }

            return true;
        }