internal void OnPacket(SIO_Packet p)
        {
            // https://socket.io/docs/client-api/#socket-on-eventName-callback
            switch (p.Type)
            {
            case SIO_Packet.PacketType.ACK:
            case SIO_Packet.PacketType.BINARY_ACK:
            case SIO_Packet.PacketType.DISCONNECT:
            case SIO_Packet.PacketType.ERROR:
                // ignore for now
                break;

            case SIO_Packet.PacketType.CONNECT:
                // Indicates we are now properly connected (to this namespace)
                break;

            case SIO_Packet.PacketType.EVENT:
                OnEvent(p);
                break;
                // maybe add support for:
                // CallHandler("connect_timeout", null)
                // CallHandler("connect_error", null)
                // CallHandler("reconnect", null)
                // CallHandler("reconnect_attempt", null)
                // CallHandler("reconnecting", null)
                // CallHandler("reconnect_error", null)
                // CallHandler("reconnect_failed", null)
                // CallHandler("ping", null)
                // CallHandler("pong", null)
            }
        }
Beispiel #2
0
        private void Engine_OnMessage(object sender, string message)
        {
            SIO_Packet p = SIO_Packet.Decode(message);

            if (namespaces.ContainsKey(p.Namespace))
            {
                namespaces[p.Namespace].OnPacket(p);
            }
            else
            {
                rootNamespace.OnPacket(p);
            }
        }
        public static SIO_Packet Decode(string s)
        {
            if (s.Length == 0 || !char.IsNumber(s.ElementAt(0)))
            {
                return(Error("Malformed packet"));
            }

            int typeID = int.Parse(s.ElementAt(0).ToString());

            if (!Enum.IsDefined(typeof(PacketType), typeID))
            {
                return(Error("Unknown packet type " + typeID));
            }

            var p = new SIO_Packet((PacketType)typeID);

            if (p.Type == PacketType.BINARY_EVENT || p.Type == PacketType.BINARY_ACK)
            {
                // ignore for now
            }

            var match = packetRegex.Match(s.Substring(1));

            if (!match.Success)
            {
                return(Error("Couldn't parse packet"));
            }

            if (match.Groups[1].Success)
            {
                p.Namespace = match.Groups[1].Value;
            }
            else
            {
                p.Namespace = "/";
            }

            if (match.Groups[2].Success)
            {
                p.ID = int.Parse(match.Groups[2].Value);
            }

            var payload = match.Groups[3].Value;

            if (JsonValue.TryParse(payload, out var jsonPayload))
            {
                p.Data = jsonPayload;
            }

            return(p);
        }
Beispiel #4
0
        public Namespace NS(string name)
        {
            if (namespaces.ContainsKey(name))
            {
                return(namespaces[name]);
            }

            var newNamespace = new Namespace(name, this);

            namespaces[name] = newNamespace;
            SIO_Packet p = new SIO_Packet(SIO_Packet.PacketType.CONNECT);

            p.Namespace = name;
            Send(p.Encode());
            return(newNamespace);
        }
        private void OnEvent(SIO_Packet p)
        {
            if (p.ID.HasValue)
            {
                //transport.Send(Ack)
            }

            var dataArray = p.Data.GetArray();
            var eventName = dataArray.GetStringAt(0);

            dataArray.RemoveAt(0);
            if (dataArray.Count == 0)
            {
                p.Data = null;
            }
            CallHandlers(eventName, p.Data);
        }
        public void Emit(string eventName, IJsonValue args, AckCallback ackCallback)
        {
            SIO_Packet p = new SIO_Packet(SIO_Packet.PacketType.EVENT);

            p.Namespace = Name;

            if (args.ValueType == JsonValueType.Array)
            {
                args.GetArray().Insert(0, JsonValue.CreateStringValue(eventName));
                p.Data = args;
            }
            else
            {
                var data = new JsonArray();
                data.Add(JsonValue.CreateStringValue(eventName));
                data.Add(args);
                p.Data = data;
            }

            client.Send(p.Encode());
        }