#sockets #webrtc #simple-peer
#сокеты #webrtc #simple-peer
Вопрос:
Я использую simple-peer для передачи видео. В моей локальной сети он работает нормально, поэтому я добавляю бесплатный сервер Google 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
});
});
Ошибка
Комментарии:
1. Можете ли вы также поместить часть подключения на стороне реакции? Например:
io.connect("http://localhost:8000", { transports: ['websocket', 'polling', 'flashsocket'] });