triniwiz / nativescript-socketio

Socket.IO for nativescript
Apache License 2.0
71 stars 31 forks source link

A lot of connexion to server ! #69

Closed kefahB closed 6 years ago

kefahB commented 6 years ago

Hi @triniwiz ,

My app send all the time connexion request to the server ! in one day i have ben received 7000 connexion from 2 users !! if i have 100 the server will crash !

I remarque this reproduce especially when my server is not reachable.

This come because i work with simulators ? when i kill the simulator the server try to reach him ?

I use the webSocket for return a notification to the client .. i create a room (with client id), when client buy some think he must wait for merchant approuve, and when merchant approuve i send à notification to the user room.

main-page

export async function pageLoaded(args: observable.EventData) {
    socketIO = new SocketIO("url?user=merchant&id=" + id, {
        query: {
            user: "merchant",
            id: id
        }
    });

     /**
     * Prevent multi connexion
     */
    if(!socketIO.connected) // normally it will dot no think .. there is a new connexion for each page loaded!
        socketIO.connect();
}

export function approuve(args)
{
         socketIO.emit("notify_client", {client_id: 123});
}

server

const app = require('express')();
const server = require("http").Server(app);
const io = require("socket.io")(server);

let i = 0;
let rooms   = [];

server.listen(3001);

io.on("connection", function (socket) {
    i++;
    let id     = socket.handshake.query.id;
    let user    = socket.handshake.query.user; // params possible client or merchant

    console.log(Object.keys( io.sockets.adapter.rooms ))

    if(user === "client")
    {
        socket.emit("conn", {});
        console.log("Connection client " + i);
        if(Object.keys( io.sockets.adapter.rooms ).indexOf(id) === -1)
        {
            socket.rooms = id;
            socket.join(id, () => {
                let room = Object.keys(io.sockets.adapter.rooms);
                rooms.push(room[1]);
                //console.log(socket.rooms); // [ <socket.id>, 'room 237' ]
            });
        }
    }

    if(user === "merchant")
    {
        socket.emit("conn", {});
        console.log("Connection merchant " + i);

        socket.on("notify_client", function (data) {
            console.log("notify_client", data['client_id']);
            switch(data.status)
            {
                case "success":
                    socket.to(data['client_id']).emit("payment_success", {});
                    console.log("success");
                    break;
                case "pending":
                    socket.to(data['client_id']).emit("payment_pending", {});
                    console.log("pending");
                    break;
                case "errors":
                    socket.to(data['client_id']).emit("payment_error", {});
                    console.log("errors");
                    break;
                case "cancel":
                    socket.to(data['client_id']).emit("payment_cancel", {});
                    console.log("cancel");
                    break;
            }
        })
    }

    //socket.disconnect();
});

Do you have some idea ?

triniwiz commented 6 years ago

Since this is during development usually the app will reconnect to the previously disconnected socket so you should try closing the socket first

kefahB commented 6 years ago

Thank you

william-levi commented 4 years ago

how to close ? is socket.disconnect() enough ?