{"size": 1238, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "Xplorer123/webrtc", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server.js", "max_issues_repo_name": "Xplorer123/webrtc", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server.js", "max_forks_repo_name": "Xplorer123/webrtc", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.7916666667, "max_line_length": 70, "alphanum_fraction": 0.6413570275}

const express = require('express');
const app = express();

var io = require('socket.io')
({
  path: '/webrtc'
})

app.use(express.static(__dirname+'/build'))
app.get('/', function(req, res){
    //res.sendFile(__dirname+'/build/index.html')
    res.send("hello")
})

const port = process.env.PORT || 4000;
const host = '0.0.0.0';
const server = app.listen(port, host, function(){
    console.log(`server listening to port : ${port}`)
})

io.listen(server);

const connectedPeers = new Map();

const peers = io.of('/webrtcPeer');

peers.on('connection', function(socket){
    console.log('socket connection established', socket.id)
    connectedPeers.set(socket.id, socket);
    peers.emit('peer-joined', Object.keys(peers.clients().sockets));

    socket.on('disconnect', function(){
        console.log('client disconnected', socket.id)
        connectedPeers.delete(socket.id);
    })

    socket.on('signal', function({from, to, payload}){
        console.log(from, to, payload);
        socket.to(to).emit('signal', {from, to, payload});
    })

    socket.on('candidate', function(data){
        //console.log(data.from, data.payload.type, data.to);
        socket.to(data.to).emit('candidate', data.from, data.payload);
    })
})
