Zemke / react-native-peerjs

PeerJS for React Native
56 stars 36 forks source link

Lost connection to server #24

Open adekunle11 opened 3 years ago

adekunle11 commented 3 years ago

Hello, I'm trying to use peerjs & webrtc to create a video chat app. I have this code in my client

export const API_URI = 'https://mvmserver.herokuapp.com/'

// Peer Config

const peerServer = new Peer(1, {
    host: 'mvmserver.herokuapp.com',
    secure: false,
    port: 443,
    path: '/mypeer'
})

peerServer.on('error', console.log)
// Socket config

export const socket = IO(`${API_URI}`, {
    forceNew: true
})

socket.on('connection', () => console.log('Connected client'))

export const joinRoom = (stream) => async (dispatch) => {
    const roomID = 'chat1_user1'
    //set my own stream
    dispatch({type: MY_STREAM, payload: stream})

    //Open a connection to our server
    peerServer.on('open', (userId) => {
        socket.emit('join-room', {userId, roomID})
    })

    socket.on('user-connected', (userId) => {
        connectToNewUser(userId, stream, dispatch)
    })

    // Receive a call
    peerServer.on('call', (call) => {
        call.answer(stream)

        //stream back the call

        call.on('stream', (stream) => {
            dispatch({type: ADD_STREAM, payload: stream})
        })
    })
};

function connectToNewUser(userId, stream, dispatch) {

    const call = peerServer.call(userId, stream);
    call.on('stream', (remoteVideoStream) => {
        if (remoteVideoStream) {
            dispatch({type: ADD_REMOTE_STREAM, payload: remoteVideoStream})
        }
    })

}

and this in my heroku server

const express = require('express')
const http = require('http')
const socketio = require('socket.io')

const {ExpressPeerServer} = require('peer');

const app = express();

const server = http.createServer(app)

const io = socketio(server).sockets

//Borderparser
app.use(express.json())

const customGenerationFunction = () =>     (Math.random().toString(36) + "0000000000000000000").substr(2, 16)

const peerServer = ExpressPeerServer(server, {
    debug: false,
    secure: false,
    path: '/',
    genderateClientId: customGenerationFunction
})

io.on('connection', function(socket) {
    console.log('connected')
    socket.on('join-room', ({roomID, userId}) => {
        socket.join(roomID)
        socket.to(roomID).broadcast.emit('user-connected', userId)
    })
})

const port = process.env.PORT || 443

server.listen(port, () => console.log(`Server is running on port ${port}`))

But i get the error, lost connection to server everytime my app launches. what could be wrong

adekunle11 commented 3 years ago

fixed, i needed to add iceServers and also, some versions of socket.io don't work well, so i downgraded to 2,1.1