|
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 | | -}) |
| 1 | +navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(function (stream) { |
| 2 | + |
| 3 | + const signalhub = require('signalhub') |
| 4 | + const createSwarm = require('webrtc-swarm') |
| 5 | + const hub = signalhub('my-game', [ |
| 6 | + 'http://localhost:8080' |
| 7 | + ]) |
| 8 | + const swarm = createSwarm(hub, { |
| 9 | + stream: stream |
| 10 | + }) |
| 11 | + |
| 12 | + const Player = require('./player.js') |
| 13 | + const you = new Player() |
| 14 | + you.addStream(stream) |
| 15 | + |
| 16 | + const players = {} |
| 17 | + swarm.on('connect', function (peer, id) { |
| 18 | + if (!players[players]) { |
| 19 | + players[id] = new Player() |
| 20 | + peer.on('data', function (data) { |
| 21 | + data = JSON.parse(data.toString()) |
| 22 | + players[id].update(data) |
| 23 | + }) |
| 24 | + players[id].addStream(peer.stream) |
| 25 | + } |
| 26 | + }) |
18 | 27 |
|
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) |
| 28 | + swarm.on('disconnect', function (peer, id) { |
| 29 | + if (players[id]) { |
| 30 | + players[id].element.parentNode.removeChild(players[id].element) |
| 31 | + delete players[id] |
| 32 | + } |
| 33 | + }) |
42 | 34 |
|
| 35 | + // hub.subscribe('update').on('data', function (data) { |
| 36 | + // if (data.color === you.color) return |
| 37 | + // if (!players[data.color]) { |
| 38 | + // players[data.color] = new Player(data) |
| 39 | + // } |
| 40 | + // players[data.color].update(data) |
| 41 | + // //console.log(data) |
| 42 | + // }) |
| 43 | + |
| 44 | + setInterval(function () { |
| 45 | + //hub.broadcast('update', window.location.hash) |
| 46 | + you.update() |
| 47 | + //hub.broadcast('update', you) |
| 48 | + const youString = JSON.stringify(you) |
| 49 | + swarm.peers.forEach(function (peer) { |
| 50 | + peer.send(youString) |
| 51 | + }) |
| 52 | + }, 100) |
| 53 | + |
| 54 | + document.addEventListener('keypress', function (e) { |
| 55 | + const speed = 16 |
| 56 | + switch (e.key) { |
| 57 | + case 'a': |
| 58 | + you.x -= speed |
| 59 | + break |
| 60 | + case 'd': |
| 61 | + you.x += speed |
| 62 | + break |
| 63 | + case 'w': |
| 64 | + you.y -= speed |
| 65 | + break |
| 66 | + case 's': |
| 67 | + you.y += speed |
| 68 | + break |
| 69 | + } |
| 70 | + }, false) |
| 71 | + |
| 72 | +}) |
43 | 73 |
|
0 commit comments