Example #1
0
 public static void onWebSocketDisconnect(NetEventIO sender)
 {
     Program.websocketPubSub.onUnsubscribeAll(sender);
     if (sender.db != null)
     {
         sender.db.Dispose();
     }
     Thread.CurrentThread.Abort();
 }
Example #2
0
        public static void getArticuloImg(NetEventIO client, string message)
        {
            short id;

            try
            {
                id = Int16.Parse(message);
            }
            catch (Exception) { return; }
            if (id <= 0)
            {
                return;
            }

            byte[] img = client.db.getArticuloImg(id);

            client.emit("artImg", "" + id, img);
        }
Example #3
0
        public static bool onWebSocketPasswordLogin(NetEventIO client, string passwd)
        {
            Console.WriteLine("processant login... ");

            // recuperar la informacio del hash enmagatzemat
            UsuarioLogin login;

            try
            {
                login = (UsuarioLogin)JsonConvert.DeserializeObject(passwd, typeof(UsuarioLogin));
            }
            catch (Exception) { return(false); }
            if (login == null || !login.isValid())
            {
                return(false);
            }

            Usuario usuario = Program.db.getUsuario(login.name);

            if (usuario == null)
            {
                return(false);
            }

            // comprovar si les contrasenyes coincideixen
            bool loggedIn = Usuario.verifyHash(usuario.pwd, (usuario.salt + login.pwd), usuario.iteraciones);

            if (loggedIn)
            {
                Console.WriteLine("login fet");
                client.db = new Storage.DBStorage(Program.config);
                client.db.updateUsuarioOnline(usuario.id);
            }
            else
            {
                Console.WriteLine("login no fet");
            }

            return(loggedIn);
        }
Example #4
0
        public static void Run()
        {
            // start listening connections
            TcpListener server = new TcpListener(System.Net.IPAddress.Any, Program.ajuste.puertoWeb);

            server.Start();

            // generate options for the future socket connections
            NetEventIOServer_Options options = new NetEventIOServer_Options(onWebSocketPasswordLogin, onWebSocketTokenLogin,
                                                                            Program.websocketPubSub.onSubscribe, Program.websocketPubSub.onUnsubscribe, Program.websocketPubSub.onUnsubscribeAll);

            Console.WriteLine("WebSocket server listening.");

            // wait for more TCP clients
            while (true)
            {
                TcpClient     client = server.AcceptTcpClient();
                NetworkStream stream = client.GetStream();

                Console.WriteLine("WebSocket client connected :D");

                new Thread(new ThreadStart(() =>
                {
                    WebSocketConnection conn = new WebSocketConnection(client, stream);
                    if (!conn.connect())
                    {
                        return;
                    }

                    NetEventIO clientIO = new NetEventIO(conn, options);
                    new Thread(new ThreadStart(clientIO.Run)).Start();

                    clientIO.onDisconnect = onWebSocketDisconnect;
                    AdminController.addEventListeners(clientIO);
                })).Start();
            }
        }
Example #5
0
 public static bool onWebSocketTokenLogin(NetEventIO client, string token, string addr)
 {
     Console.WriteLine("processant token... " + token + " " + addr);
     return(false);
 }
Example #6
0
        public static void addEventListeners(NetEventIO clientIO)
        {
            clientIO.on("usuarios", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(UsuariosController.usuarioCommands(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("robot", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(RobotController.robotCommands(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("ordenMinado", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(OrdenMinadoController.ordenesCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("articulos", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(ArticulosController.articulosCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("articulos", (NetEventIO sender, OnMessageEventArgs msg) =>
            {
                switch (msg.message.command)
                {
                case "getImg":
                    {
                        ArticulosController.getArticuloImg(sender, msg.message.message);
                        break;
                    }
                }
            });

            clientIO.on("articuloSetImg", (NetEventIO sender, BinaryMessage msg) =>
            {
                ArticulosController.setArticuloImg(sender.db, msg);
            });

            clientIO.on("electrico", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(RedElectricaController.redElectricaCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("almacen", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(AlmacenController.almacenCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("ajustes", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(AjustesController.ajustesCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("apiKey", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(AjustesController.apiKeyCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("server", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(AjustesController.serverCommand(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("drone", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(DroneController.droneCommands(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("movAlmacen", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(MovimientosAlmacenController.movimientosAlmacenCommands(sender.db, msg.message.command, msg.message.message));
            });

            clientIO.on("notificacion", (NetEventIO sender, OnMessageEventArgs msg, NetEventIO.Callback callback) =>
            {
                callback(NotificacionesController.notificacionesCommands(sender.db, msg.message.command, msg.message.message));
            });
        }