1

我正在使用 simple-peer 进行视频传输。它在我的本地网络中工作正常,所以我添加了谷歌的免费 stun 服务器来与不在我本地网络中的人连接。但是,如果我尝试连接到本地网络之外的任何人并在本地网络上正常工作,它的抛出错误连接就会失败。

反应部分

const peer = new Peer({
            initiator: true,
            trickle: false,
            stream, 
            config: {
                iceServers: [
                    {urls: 'stun:stun.l.google.com:19302'},
                    { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }
                ]
            }
        });

        peer.on("signal", signal => {
            socketRef.current.emit("sendingSignal", { userIdToSendSignal: userIdToSendSignal, callerId: callerId, signal });
        })
})

const peer = new Peer({
            initiator: false,
            trickle: false,
            stream,
            config: {
                iceServers: [
                    { urls: 'stun:stun.l.google.com:19302' },
                    { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }
                ]
            },
        });

        //other peer give its signal in signal object and this peer returning its own signal
        peer.on("signal", signal => {
            socketRef.current.emit("returningSignal", { signal, callerId: callerId });
        });
        peer.signal(incomingSignal);
})

Nodejs部分

const socket = require("socket.io");
const io = socket(server);

const usersInRoom = {}; //all user(socket id) connected to a chatroom
const socketToRoom = {}; //roomId in which a socket id is connected

//verifying token
io.use(async (socket, next) => {
    try {
        const token = socket.handshake.query.token;
        const payload = await jwt.verify(token, process.env.SECRET);
        socket.userId = payload;
        const user = await User.findOne({ _id: socket.userId }).select('-password');
        socket.username = user.username;
        socket.name = user.name;
        next();
    } catch (error) {
        console.log(error);
    }
});

io.on('connection', socket => {
    console.log('Some one joined socketId: ' + socket.id);
    socket.on("joinRoom", roomId=> {
        console.log('Joined roomId: ' + roomId + " socketId: " + socket.id + ' userId: ' + socket.userId);
        if (usersInRoom[roomId]) {
            const length = usersInRoom[roomId].length;
            usersInRoom[roomId].push(socket.id);
        } else {
            usersInRoom[roomId] = [socket.id];
        }
        socketToRoom[socket.id] = roomId;
        const usersInThisRoom = usersInRoom[roomId].filter(id => id !== socket.id);
        socket.join(roomId); //for message
        socket.emit("usersInRoom", usersInThisRoom); //send all socket id connected to this room
    });

    //client send this signal to sever and sever will send to other user of peerId(callerId is peer id)
    socket.on("sendingSignal", payload => {
        console.log(payload.callerId);
        io.to(payload.userIdToSendSignal).emit('userJoined', { signal: payload.signal, callerId: payload.callerId });
    });

    //client site receive signal of other peer and it sending its own signal for other member
    socket.on("returningSignal", payload => {
        io.to(payload.callerId).emit('takingReturnedSignal', { signal: payload.signal, id: socket.id });
    });

    //from client send message to send all other connected user of same room
    socket.on('sendMessage', payload => {
        //sending message to all other connected user at same room
        io.to(payload.roomId).emit('receiveMessage', { message: payload.message, name:socket.name, username: socket.username });
    });

    //someone left room
    socket.on('disconnect', () => {
        const roomId = socketToRoom[socket.id];
        let socketsIdConnectedToRoom = usersInRoom[roomId];
        if (socketsIdConnectedToRoom) {
            socketsIdConnectedToRoom = socketsIdConnectedToRoom.filter(id => id !== socket.id);
            usersInRoom[roomId] = socketsIdConnectedToRoom;
        }
        socket.leave(roomId); //for message group(socket)
        socket.broadcast.emit("userLeft", socket.id); //sending socket id to all other connected user of same room without its own
    });
});

错误

在此处输入图像描述

4

0 回答 0