Node.js 服务器中的Socket.io反复断开连接,而客户端没有';T

Node.js 服务器中的Socket.io反复断开连接,而客户端没有';T,node.js,react-native,socket.io,Node.js,React Native,Socket.io,我要做一个像WhatsApp这样的私人聊天应用。 我成功连接到服务器 但是套接字在几秒钟后与服务器断开连接。 在客户端上,它不会断开连接 服务器代码: const app = require('express')(); const server = require('http').Server(app); const io = require('socket.io')(server); const port = 3000; app.get('/', (req, res) => {

我要做一个像WhatsApp这样的私人聊天应用。 我成功连接到服务器 但是套接字在几秒钟后与服务器断开连接。 在客户端上,它不会断开连接

服务器代码:

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

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

const onlineusers = {};
const socketid = {};

io.on('connection', cs => {

    cs.on('online', username => {
        if(username){
            onlineusers[username] = cs.id;
            socketid[cs.id] = username;
        }

        console.log("\nonline: ", onlineusers);
    });

    cs.on('disconnect', () => {

        delete onlineusers[socketid[cs.id]];

        console.log("\noffline: ", onlineusers);
    });

});

const chat = io.of("/chat");

chat.on('connection', cs => {

    cs.on('startchat', username => {
        if (username){
            chat.to('/chat#'+onlineusers[username]).emit('hey', 'I love programming');
        }
    });

});

server.listen(port, err => {
    if(err){
        console.error("Some Error: "+err);
    }else{
        console.log(`Server is running on port: ${port}`);
    }
});
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000';

this.socket = io(SocketEndpoint, {
            transports: ['websocket']
        });
this.socket.on('connect', () => {

            if (this.state.username) {
                this.socket.emit("online", this.state.username);
            }

        });

 this.socket.on('connect_error', (err) => {
     Alert.alert(err);
 });

 this.socket.on('disconnect', () => {
     Alert.alert('disconnected');
 });
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000/chat';

this.socket = io(SocketEndpoint, {
                transports: ['websocket']
            });

this.socket.on('connect', () => {

            if (theirusername) {
                this.socket.emit('startchat', theirusername);
            }

            this.socket.on('hey', data => {
                alert(data);
            });

            this.socket.on('janajan', data => {
                alert(data);
            });

        });
我的客户端代码由react native和socket生成。io客户端:

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

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

const onlineusers = {};
const socketid = {};

io.on('connection', cs => {

    cs.on('online', username => {
        if(username){
            onlineusers[username] = cs.id;
            socketid[cs.id] = username;
        }

        console.log("\nonline: ", onlineusers);
    });

    cs.on('disconnect', () => {

        delete onlineusers[socketid[cs.id]];

        console.log("\noffline: ", onlineusers);
    });

});

const chat = io.of("/chat");

chat.on('connection', cs => {

    cs.on('startchat', username => {
        if (username){
            chat.to('/chat#'+onlineusers[username]).emit('hey', 'I love programming');
        }
    });

});

server.listen(port, err => {
    if(err){
        console.error("Some Error: "+err);
    }else{
        console.log(`Server is running on port: ${port}`);
    }
});
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000';

this.socket = io(SocketEndpoint, {
            transports: ['websocket']
        });
this.socket.on('connect', () => {

            if (this.state.username) {
                this.socket.emit("online", this.state.username);
            }

        });

 this.socket.on('connect_error', (err) => {
     Alert.alert(err);
 });

 this.socket.on('disconnect', () => {
     Alert.alert('disconnected');
 });
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000/chat';

this.socket = io(SocketEndpoint, {
                transports: ['websocket']
            });

this.socket.on('connect', () => {

            if (theirusername) {
                this.socket.emit('startchat', theirusername);
            }

            this.socket.on('hey', data => {
                alert(data);
            });

            this.socket.on('janajan', data => {
                alert(data);
            });

        });
在线用户文件:

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

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

const onlineusers = {};
const socketid = {};

io.on('connection', cs => {

    cs.on('online', username => {
        if(username){
            onlineusers[username] = cs.id;
            socketid[cs.id] = username;
        }

        console.log("\nonline: ", onlineusers);
    });

    cs.on('disconnect', () => {

        delete onlineusers[socketid[cs.id]];

        console.log("\noffline: ", onlineusers);
    });

});

const chat = io.of("/chat");

chat.on('connection', cs => {

    cs.on('startchat', username => {
        if (username){
            chat.to('/chat#'+onlineusers[username]).emit('hey', 'I love programming');
        }
    });

});

server.listen(port, err => {
    if(err){
        console.error("Some Error: "+err);
    }else{
        console.log(`Server is running on port: ${port}`);
    }
});
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000';

this.socket = io(SocketEndpoint, {
            transports: ['websocket']
        });
this.socket.on('connect', () => {

            if (this.state.username) {
                this.socket.emit("online", this.state.username);
            }

        });

 this.socket.on('connect_error', (err) => {
     Alert.alert(err);
 });

 this.socket.on('disconnect', () => {
     Alert.alert('disconnected');
 });
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000/chat';

this.socket = io(SocketEndpoint, {
                transports: ['websocket']
            });

this.socket.on('connect', () => {

            if (theirusername) {
                this.socket.emit('startchat', theirusername);
            }

            this.socket.on('hey', data => {
                alert(data);
            });

            this.socket.on('janajan', data => {
                alert(data);
            });

        });
聊天页面文件:

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

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

const onlineusers = {};
const socketid = {};

io.on('connection', cs => {

    cs.on('online', username => {
        if(username){
            onlineusers[username] = cs.id;
            socketid[cs.id] = username;
        }

        console.log("\nonline: ", onlineusers);
    });

    cs.on('disconnect', () => {

        delete onlineusers[socketid[cs.id]];

        console.log("\noffline: ", onlineusers);
    });

});

const chat = io.of("/chat");

chat.on('connection', cs => {

    cs.on('startchat', username => {
        if (username){
            chat.to('/chat#'+onlineusers[username]).emit('hey', 'I love programming');
        }
    });

});

server.listen(port, err => {
    if(err){
        console.error("Some Error: "+err);
    }else{
        console.log(`Server is running on port: ${port}`);
    }
});
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000';

this.socket = io(SocketEndpoint, {
            transports: ['websocket']
        });
this.socket.on('connect', () => {

            if (this.state.username) {
                this.socket.emit("online", this.state.username);
            }

        });

 this.socket.on('connect_error', (err) => {
     Alert.alert(err);
 });

 this.socket.on('disconnect', () => {
     Alert.alert('disconnected');
 });
import io from 'socket.io-client';
const SocketEndpoint = 'http://192.168.43.172:3000/chat';

this.socket = io(SocketEndpoint, {
                transports: ['websocket']
            });

this.socket.on('connect', () => {

            if (theirusername) {
                this.socket.emit('startchat', theirusername);
            }

            this.socket.on('hey', data => {
                alert(data);
            });

            this.socket.on('janajan', data => {
                alert(data);
            });

        });
我想在服务器上保留客户端套接字,直到客户端自己断开连接。 因为在这里,当我想说“嘿”的时候,它会断开连接,我的消息会传递给客户端。
之前谢谢你

所以服务器断开并关闭套接字,但客户端仍然连接?是的,从服务器断开,但从服务器断开。因为此函数在断开连接时不调用。this.socket.on('disconnect',()=>{Alert.Alert('disconnected');});是否为每个页面建立新连接?在页面之间导航时,socket.io断开连接。在我以前的聊天应用中,我使用的是一个长连接。谢谢,你能告诉我如何创建这个长连接套接字吗。我正在使用没有redux的react导航,我不知道如何在应用程序中保留一个套接字实例。