Skip to content

Commit 33f3d03

Browse files
committed
Simple P2P with WebRTC
1 parent 5b74ab4 commit 33f3d03

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

simple-p2p-with-webrtc/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Simple P2P with WebRTC
2+
3+
> [https://youtu.be/jY9k4rfXwEI](https://youtu.be/jY9k4rfXwEI)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `npm start` to run the app viewable on `http://localhost:9966`.

simple-p2p-with-webrtc/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const signalhub = require('signalhub')
2+
const hub = signalhub('my-game', [
3+
'http://localhost:8080'
4+
])
5+
6+
const Player = require('./player.js')
7+
const you = new Player()
8+
9+
const players = {}
10+
hub.subscribe('update').on('data', function (data) {
11+
if (data.color === you.color) return
12+
if (!players[data.color]) {
13+
players[data.color] = new Player(data)
14+
}
15+
players[data.color].update(data)
16+
//console.log(data)
17+
})
18+
19+
setInterval(function () {
20+
//hub.broadcast('update', window.location.hash)
21+
you.update()
22+
hub.broadcast('update', you)
23+
}, 100)
24+
25+
document.addEventListener('keypress', function (e) {
26+
const speed = 16
27+
switch (e.key) {
28+
case 'a':
29+
you.x -= speed
30+
break
31+
case 'd':
32+
you.x += speed
33+
break
34+
case 'w':
35+
you.y -= speed
36+
break
37+
case 's':
38+
you.y += speed
39+
break
40+
}
41+
}, false)
42+
43+

simple-p2p-with-webrtc/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "simple-p2p-with-webrtc",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js",
8+
"signalhub": "signalhub listen -p 8080"
9+
},
10+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"budo": "^11.2.0"
14+
},
15+
"dependencies": {
16+
"signalhub": "^4.9.0"
17+
}
18+
}

simple-p2p-with-webrtc/player.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = Player
2+
3+
function Player (data) {
4+
data = data || {}
5+
this.color = data.color || randomColor()
6+
this.x = 0
7+
this.y = 0
8+
this.element = document.createElement('div')
9+
Object.assign(this.element.style, {
10+
width: '16px',
11+
height: '16px',
12+
position: 'absolute',
13+
top: '0px',
14+
left: '0px',
15+
backgroundColor: this.color
16+
})
17+
document.body.appendChild(this.element)
18+
}
19+
20+
Player.prototype.update = function (data) {
21+
data = data || {}
22+
this.x = data.x || this.x
23+
this.y = data.y || this.y
24+
Object.assign(this.element.style, {
25+
top: this.y + 'px',
26+
left: this.x + 'px'
27+
})
28+
}
29+
30+
function randomColor () {
31+
return '#' + ((1 << 24) * Math.random() | 0).toString()
32+
}

0 commit comments

Comments
 (0)