Ejemplo n.º 1
0
        public static EventMessage Deserialize(string rawMessage)
        {
			EventMessage evtMsg = new EventMessage();
            //  '5:' [message id ('+')] ':' [message endpoint] ':' [json encoded event]
            //   5:1::{"a":"b"}
			evtMsg.RawMessage = rawMessage;
			try
			{
				string[] args = rawMessage.Split(SPLITCHARS, 4); // limit the number of pieces
				if (args.Length == 4)
				{
					int id;
					if (int.TryParse(args[1], out id))
						evtMsg.AckId = id;
					evtMsg.Endpoint = args[2];
					evtMsg.MessageText = args[3];

					if (!string.IsNullOrEmpty(evtMsg.MessageText) &&
						evtMsg.MessageText.Contains("name") &&
						evtMsg.MessageText.Contains("args"))
					{
						evtMsg.Json = JsonEncodedEventMessage.Deserialize(evtMsg.MessageText);
						evtMsg.Event = evtMsg.Json.Name;
					}
					else
						evtMsg.Json = new JsonEncodedEventMessage();
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex);
			}
			return evtMsg;
        }
Ejemplo n.º 2
0
		/// <summary>
		/// <para>Asynchronously sends payload using eventName</para>
		/// <para>payload must a string or Json Serializable</para>
		/// <para>Mimicks Socket.IO client 'socket.emit('name',payload);' pattern</para>
		/// <para>Do not use the reserved socket.io event names: connect, disconnect, open, close, error, retry, reconnect</para>
		/// </summary>
		/// <param name="eventName"></param>
		/// <param name="payload">must be a string or a Json Serializable object</param>
		/// <remarks>ArgumentOutOfRangeException will be thrown on reserved event names</remarks>
		public void Emit(string eventName, dynamic payload, string endPoint = "", Action<dynamic> callback = null)
		{

			string lceventName = eventName.ToLower();
			IMessage msg = null;
			switch (lceventName)
			{
				case "message":
					if (payload is string)
						msg = new TextMessage() { MessageText = payload };
					else
						msg = new JSONMessage(payload);
					this.Send(msg);
					break;
				case "connect":
				case "disconnect":
				case "open":
				case "close":
				case "error":
				case "retry":
				case "reconnect":
					throw new System.ArgumentOutOfRangeException(eventName, "Event name is reserved by socket.io, and cannot be used by clients or servers with this message type");
				default:
					if (!string.IsNullOrWhiteSpace(endPoint) && !endPoint.StartsWith("/"))
						endPoint = "/" + endPoint;
					msg = new EventMessage(eventName, payload, endPoint, callback);
					if (callback != null)
						this.registrationManager.AddCallBack(msg);

					this.Send(msg);
					break;
			}
		}