File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ` .
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments