Beispiel #1
0
		void SocketMessageReceivedFunction (object o, MessageReceivedEventArgs e)
		{
			Debug.WriteLine ("Received Message: {0}", e.Message);

			TimeoutTimer.Change (TimeoutTime, Timeout.Infinite);

			var match = Regex.Match (e.Message, socketIOEncodingPattern);

			var messageType = (MessageType)int.Parse (match.Groups [1].Value);
			var messageId = match.Groups [2].Value;
			var endpoint = match.Groups [3].Value;

			var socketMessageInfo = new MessageID (messageId, endpoint);
			var	data = match.Groups [4].Value;

			if (!string.IsNullOrEmpty (data))
				data = data.Substring (1); //ignore leading ':'

			JObject jObjData = null;

			switch (messageType) {

			case MessageType.Disconnect:
				Debug.WriteLine ("Disconnected");
				SocketDisconnected (socketMessageInfo, endpoint);
				break;

			case MessageType.Connect:
				Debug.WriteLine ("Connected");
				SocketConnected (socketMessageInfo, endpoint);
				break;

			case MessageType.Heartbeat:
				Debug.WriteLine ("Heartbeat");
				SendHeartbeat ();
				break;

			case MessageType.Message:
				Debug.WriteLine ("Message = {0}", data);
				SocketReceivedMessage (socketMessageInfo, data);
				break;

			case MessageType.Json:
				Debug.WriteLine ("Json = {0}", data);
				if (!string.IsNullOrEmpty (data))
					jObjData = JObject.Parse (data);
				SocketReceivedJson (socketMessageInfo, jObjData);
				break;

			case MessageType.Event:
				Debug.WriteLine ("Event");
				string eventName = "";
				if (!string.IsNullOrEmpty (data)) {
					jObjData = JObject.Parse (data);
					eventName = jObjData ["name"].ToString ();
				}

				if (!string.IsNullOrEmpty (eventName) && EventHandlers.ContainsKey (eventName)) {
					var handlers = EventHandlers [eventName];
					foreach (var handler in handlers) {
						if (handler != null) {
							JArray args = null;
							if (jObjData != null && jObjData["args"] != null)
								args = JArray.Parse (jObjData ["args"].ToString ());
							handler (args.First);
							Debug.WriteLine ("event: {0} with args: {1}", eventName, args);
						}
					}
				}
				break;

			case MessageType.Ack:
				Debug.WriteLine ("Ack");
				if (!string.IsNullOrEmpty (data)) {
					var ackMatch = Regex.Match (data, socketAckEncodingPattern);
					var ackMessageId = int.Parse (ackMatch.Groups [1].Value);
					var ackData = ackMatch.Groups [2].Value;
					JArray jData = null;

					if (!string.IsNullOrEmpty (ackData)) {
						ackData = ackData.Substring (1); //ignore leading '+'
						jData = JArray.Parse (ackData);
					}
					SocketReceivedAcknowledgement (ackMessageId, jData);
				}
				break;

			case MessageType.Error:
				Debug.WriteLine ("Error");
				SocketReceivedError (socketMessageInfo, data);
				break;

			case MessageType.Noop:
				Debug.WriteLine ("Noop");
				break;

			default:
				Debug.WriteLine ("Something went wrong here...");
				if (!string.IsNullOrEmpty (data)) {
					jObjData = JObject.Parse (data);
					Debug.WriteLine ("jObjData = {0}", jObjData.ToString ());
				}
				break;
			}

		}
Beispiel #2
0
        void SocketMessageReceivedFunction(object o, MessageReceivedEventArgs e)
        {
            Debug.WriteLine("Received Message: {0}", e.Message);

            TimeoutTimer.Change(TimeoutTime, Timeout.Infinite);

            var match = Regex.Match(e.Message, socketIOEncodingPattern);

            var messageType = (MessageType)int.Parse(match.Groups [1].Value);
            var messageId   = match.Groups [2].Value;
            var endpoint    = match.Groups [3].Value;

            var socketMessageInfo = new MessageID(messageId, endpoint);
            var data = match.Groups [4].Value;

            if (!string.IsNullOrEmpty(data))
            {
                data = data.Substring(1);                  //ignore leading ':'
            }
            JObject jObjData = null;

            switch (messageType)
            {
            case MessageType.Disconnect:
                Debug.WriteLine("Disconnected");
                SocketDisconnected(socketMessageInfo, endpoint);
                break;

            case MessageType.Connect:
                Debug.WriteLine("Connected");
                SocketConnected(socketMessageInfo, endpoint);
                break;

            case MessageType.Heartbeat:
                Debug.WriteLine("Heartbeat");
                SendHeartbeat();
                break;

            case MessageType.Message:
                Debug.WriteLine("Message = {0}", data);
                SocketReceivedMessage(socketMessageInfo, data);
                break;

            case MessageType.Json:
                Debug.WriteLine("Json = {0}", data);
                if (!string.IsNullOrEmpty(data))
                {
                    jObjData = JObject.Parse(data);
                }
                SocketReceivedJson(socketMessageInfo, jObjData);
                break;

            case MessageType.Event:
                Debug.WriteLine("Event");
                string eventName = "";
                if (!string.IsNullOrEmpty(data))
                {
                    jObjData  = JObject.Parse(data);
                    eventName = jObjData ["name"].ToString();
                }

                if (!string.IsNullOrEmpty(eventName) && EventHandlers.ContainsKey(eventName))
                {
                    var handlers = EventHandlers [eventName];
                    foreach (var handler in handlers)
                    {
                        if (handler != null)
                        {
                            JArray args = null;
                            if (jObjData != null && jObjData["args"] != null)
                            {
                                args = JArray.Parse(jObjData ["args"].ToString());
                            }
                            handler(args.First);
                            Debug.WriteLine("event: {0} with args: {1}", eventName, args);
                        }
                    }
                }
                break;

            case MessageType.Ack:
                Debug.WriteLine("Ack");
                if (!string.IsNullOrEmpty(data))
                {
                    var    ackMatch     = Regex.Match(data, socketAckEncodingPattern);
                    var    ackMessageId = int.Parse(ackMatch.Groups [1].Value);
                    var    ackData      = ackMatch.Groups [2].Value;
                    JArray jData        = null;

                    if (!string.IsNullOrEmpty(ackData))
                    {
                        ackData = ackData.Substring(1);                          //ignore leading '+'
                        jData   = JArray.Parse(ackData);
                    }
                    SocketReceivedAcknowledgement(ackMessageId, jData);
                }
                break;

            case MessageType.Error:
                Debug.WriteLine("Error");
                SocketReceivedError(socketMessageInfo, data);
                break;

            case MessageType.Noop:
                Debug.WriteLine("Noop");
                break;

            default:
                Debug.WriteLine("Something went wrong here...");
                if (!string.IsNullOrEmpty(data))
                {
                    jObjData = JObject.Parse(data);
                    Debug.WriteLine("jObjData = {0}", jObjData.ToString());
                }
                break;
            }
        }