Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mac authored and Mac committed Sep 1, 2019
0 parents commit 384cdd2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

package-lock.json
54 changes: 54 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(function (stream) {
//This is used for Signaling the Peer
const signalhub = require('signalhub')
const createSwarm = require('webrtc-swarm')
//Creates the Signal rub running in the mentioned port
const hub = signalhub('my-game', [
'http://localhost:8080'
])
const swarm = createSwarm(hub, {
stream: stream
})
//Creates a video player
const Player = require('./videoplayer.js')
const you = new Player({ x: 0, y : 0 ,color : 'black',left : 0,top : 0})
you.addStream(stream)

const players = {}
swarm.on('connect', function (peer, id) {
if (!players[id]) {
players[id] = new Player({
x : 300,
y : 0,
left : 200,
top : 0,
color : 'red'
})
peer.on('data', function (data) {
data = JSON.parse(data.toString())
players[id].update(data)
})
players[id].addStream(peer.stream)
}
})
//On webRTC Disconnets
swarm.on('disconnect', function (peer, id) {
if (players[id]) {
players[id].element.parentNode.removeChild(players[id].element)
delete players[id]
}
})


setInterval(function () {
console.log("Interval Call");
you.update()

const youString = JSON.stringify(you)
swarm.peers.forEach(function (peer) {
peer.send(youString)
})
}, 100)
})

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "node-p2p",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "budo index.js",
"signalhub": "signalhub listen -p 8080",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"budo": "^11.6.3"
},
"dependencies": {
"signalhub": "^4.9.0",
"webrtc-swarm": "^2.9.0"
}
}
40 changes: 40 additions & 0 deletions videoplayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = Player

function Player (data) {
data = data || {}
this.color = data.color || randomColor()
this.x = data.x;
this.y = data.y;
this.top = data.top;
this.left = data.left;
this.name = data.name;
this.element = document.createElement('video')
Object.assign(this.element.style, {
width: '40%',
height: '50%',
position: 'absolute',
top: data.top+'px',
left: data.left+'px',
backgroundColor: this.color
})
document.body.appendChild(this.element)
}

Player.prototype.addStream = function (stream) {
this.element.srcObject = stream
this.element.play()
}

Player.prototype.update = function (data) {
data = data || {}
this.x = data.x || this.x
this.y = data.y || this.y
Object.assign(this.element.style, {
top: this.y + 'px',
left: this.x + 'px'
})
}

function randomColor () {
return '#' + Math.random().toString(16).substr(-6)
}

0 comments on commit 384cdd2

Please sign in to comment.