Пример #1
0
        public string Encode(Packet packet)
        {
            try
            {
#if SOCKET_IO_DEBUG
				Debug.Log("[SocketIO] Encoding: " + packet.json);
				#endif

                var builder = new StringBuilder();

                // first is type
                builder.Append((int) packet.enginePacketType);
                builder.Append((int) packet.socketPacketType);

                // attachments if we have them
                if (packet.socketPacketType == SocketPacketType.BINARY_EVENT ||
                    packet.socketPacketType == SocketPacketType.BINARY_ACK)
                {
                    builder.Append(packet.attachments);
                    builder.Append('-');
                }

                // if we have a namespace other than '/'
                // we append it followed by a comma ','
                if (!string.IsNullOrEmpty(packet.nsp) && !packet.nsp.Equals("/"))
                {
                    builder.Append(packet.nsp);
                    builder.Append(',');
                }

                // immediately followed by the id
                if (packet.id > -1)
                {
                    builder.Append(packet.id);
                }

                if (packet.json != null)
                {
                    builder.Append(packet.json.ToString());
                }

#if SOCKET_IO_DEBUG
				Debug.Log("[SocketIO] Encoded: " + builder);
				#endif

                return builder.ToString();
            }
            catch (Exception ex)
            {
                throw new SocketIOException("Packet encoding failed: " + packet, ex);
            }
        }
Пример #2
0
		private void InvokeAck(Packet packet)
		{
			Ack ack;
			for(int i = 0; i < ackList.Count; i++){
				if(ackList[i].packetId != packet.id){ continue; }
				ack = ackList[i];
				ackList.RemoveAt(i);
				ack.Invoke(packet.json);
				return;
			}
		}
Пример #3
0
		private void HandleMessage(Packet packet)
		{
			if(packet.json == null) { return; }

			if(packet.socketPacketType == SocketPacketType.ACK){
				for(int i = 0; i < ackList.Count; i++){
					if(ackList[i].packetId != packet.id){ continue; }
					lock(ackQueueLock){ ackQueue.Enqueue(packet); }
					return;
				}

				#if SOCKET_IO_DEBUG
				debugMethod.Invoke("[SocketIO] Ack received for invalid Action: " + packet.id);
				#endif
			}

			if (packet.socketPacketType == SocketPacketType.EVENT) {
				SocketIOEvent e = parser.Parse(packet.json);
				lock(eventQueueLock){ eventQueue.Enqueue(e); }
			}
		}
Пример #4
0
		private void HandleOpen(Packet packet)
		{
			#if SOCKET_IO_DEBUG
			debugMethod.Invoke("[SocketIO] Socket.IO sid: " + packet.json["sid"].str);
			#endif
			sid = packet.json["sid"].str;
			EmitEvent("open");
		}
Пример #5
0
		private void EmitPacket(Packet packet)
		{
			#if SOCKET_IO_DEBUG
			debugMethod.Invoke("[SocketIO] " + packet);
			#endif
			
			try {
				ws.Send(encoder.Encode(packet));
			} catch(SocketIOException ex) {
				#if SOCKET_IO_DEBUG
				debugMethod.Invoke(ex.ToString());
				#endif
			}
		}
Пример #6
0
		void HandleMessage(Packet packet)
		{
			if (packet.json == null) {
				return;
			}

			if(packet.socketPacketType == SocketPacketType.ACK){
				if(!acknowledges.ContainsKey(packet.id)){ 
					#if SOCKET_IO_DEBUG
					Debug.Log("[SocketIO] Ack received for invalid Action: " + packet.id);
					#endif
					return; 
				}

				Action<JSONObject> ack = acknowledges[packet.id];
				acknowledges.Remove(packet.id);
				ack.Invoke(packet.json);
				return;
			}

			if (packet.socketPacketType == SocketPacketType.EVENT) {
				SocketIOEvent e = parser.Parse (packet.json);
				EmitEvent (e);
			}
		}
Пример #7
0
		void HandleOpen(Packet packet)
		{
			if (sid == null) {
				#if SOCKET_IO_DEBUG
				Debug.Log("[SocketIO] Socket.IO sid: " + packet.json["sid"].str);
				#endif
				sid = packet.json ["sid"].str;
			}
			EmitEvent("open");
		}
Пример #8
0
		private void EmitPacket(int id, string raw)
        {
			Packet packet = new Packet(EnginePacketType.MESSAGE, SocketPacketType.EVENT, 0, "/", id, new JSONObject(raw));
			try {
				ws.Send(encoder.Encode(packet));
			} catch(SocketIOException ex) {
				Debug.LogException(ex);
			}
		}
Пример #9
0
		public Packet Decode(MessageEventArgs e)
		{
			try
			{
				#if SOCKET_IO_DEBUG
				Debug.Log("[SocketIO] Decoding: " + e.Data);
				#endif

				string data = e.Data;
				Packet packet = new Packet();
				int offset = 0;

				// look up packet type
				int enginePacketType = int.Parse(data.Substring(offset, 1));
				packet.enginePacketType = (EnginePacketType)enginePacketType;

				if (enginePacketType == (int)EnginePacketType.MESSAGE) {
					int socketPacketType = int.Parse(data.Substring(++offset, 1));
					packet.socketPacketType = (SocketPacketType)socketPacketType;
				}

				// connect message properly parsed
				if (data.Length <= 2) {
					#if SOCKET_IO_DEBUG
					Debug.Log("[SocketIO] Decoded: " + packet);
					#endif
					return packet;
				}

				// look up namespace (if any)
				if ('/' == data [offset + 1]) {
					StringBuilder builder = new StringBuilder();
					while (offset < data.Length - 1 && data[++offset] != ',') {
						builder.Append(data [offset]);
					}
					packet.nsp = builder.ToString();
				} else {
					packet.nsp = "/";
				}

				// look up id
				char next = data [offset + 1];
				if (next != ' ' && char.IsNumber(next)) {
					StringBuilder builder = new StringBuilder();
					while (offset < data.Length - 1) {
						char c = data [++offset];
						if (char.IsNumber(c)) {
							builder.Append(c);
						} else {
							--offset;
							break;
						}
					}
					packet.id = int.Parse(builder.ToString());
				}

				// look up json data
				if (++offset < data.Length - 1) {
					try {
						#if SOCKET_IO_DEBUG
						Debug.Log("[SocketIO] Parsing JSON: " + data.Substring(offset));
						#endif
						packet.json = new JSONObject(data.Substring(offset));
					} catch (Exception ex) {
						Debug.LogException(ex);
					}
				}

				#if SOCKET_IO_DEBUG
				Debug.Log("[SocketIO] Decoded: " + packet);
				#endif

				return packet;

			} catch(Exception ex) {
				throw new SocketIOException("Packet decoding failed: " + e.Data ,ex);
			}
		}
Пример #10
0
        public void EmitPacket(Packet packet)
        {
            Info("[SocketIO]EmitPacket " + packet);

            try
            {
                ws.Send(encoder.Encode(packet));
            }
            catch (SocketIOException ex)
            {
                Error(ex.ToString());
            }
        }
Пример #11
0
 /// <summary>
 /// Unity thread safe
 /// </summary>
 /// <param name="packet"></param>
 private void InvokeAck(Packet packet)
 {
     foreach (var ack in _ackList)
     {
         if (ack.packetId != packet.id)
         {
             // Ignore 
             continue;
         }
         _ackList.Remove(ack);
         ack.Invoke(packet.json.list[0]);
         return;
     }
 }
Пример #12
0
        private void HandleMessage(Packet packet)
        {
            if (packet.json == null) { return; }

            if (packet.socketPacketType == SocketPacketType.ACK)
            {
                if (_ackList.Any(t => t.packetId == packet.id))
                {
                    // Packet received is an ack we're waiting for
                    lock (_ackQueueLock)
                    {
                        _ackQueue.Enqueue(packet);
                    }
                    return;
                }

                #if SOCKET_IO_DEBUG
                _debugMethod.Invoke("[SocketIO] Ack received for invalid Action: " + packet.id +", List length: " + _ackList.Count);
                #endif
            }

            if (packet.socketPacketType == SocketPacketType.EVENT)
            {
                try
                {
                    SocketIOEvent e = _parser.Parse(packet.json);
                    lock (_eventQueueLock)
                    {
                        _eventQueue.Enqueue(e);
                    }
                }
                catch (Exception exc)
                {
                    Debug.Log("Failed to parse a message: " + packet.json);
                    Debug.Log(exc.Message);
                }
            }
        }