Last active
          September 13, 2022 02:33 
        
      - 
      
 - 
        
Save adammw/d9bf021c395835427aa0 to your computer and use it in GitHub Desktop.  
    simple-peer / socket.io test
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var Peer = require('simple-peer'); | |
| var io = require('socket.io-client'); | |
| var debug = require('debug')('client'); | |
| var socket = io.connect(); | |
| var peers = {}; | |
| var useTrickle = true; | |
| socket.on('connect', function() { | |
| debug('Connected to signalling server, Peer ID: %s', socket.id); | |
| }); | |
| socket.on('peer', function(data) { | |
| var peerId = data.peerId; | |
| var peer = new Peer({ initiator: data.initiator, trickle: useTrickle }); | |
| debug('Peer available for connection discovered from signalling server, Peer ID: %s', peerId); | |
| socket.on('signal', function(data) { | |
| if (data.peerId == peerId) { | |
| debug('Received signalling data', data, 'from Peer ID:', peerId); | |
| peer.signal(data.signal); | |
| } | |
| }); | |
| peer.on('signal', function(data) { | |
| debug('Advertising signalling data', data, 'to Peer ID:', peerId); | |
| socket.emit('signal', { | |
| signal: data, | |
| peerId: peerId | |
| }); | |
| }); | |
| peer.on('error', function(e) { | |
| debug('Error sending connection to peer %s:', peerId, e); | |
| }); | |
| peer.on('connect', function() { | |
| debug('Peer connection established'); | |
| peer.send("hey peer"); | |
| }); | |
| peer.on('data', function(data) { | |
| debug('Recieved data from peer:', data); | |
| }); | |
| peers[peerId] = peer; | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <!doctype html> | |
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <script src="/js/bundle.js"></script> | |
| </body> | |
| </html> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "name": "simple-peer-test", | |
| "version": "1.0.0", | |
| "description": "", | |
| "dependencies": { | |
| "browserify-middleware": "^6.0.0", | |
| "debug": "^2.2.0", | |
| "express": "^4.13.1", | |
| "lodash": "^3.10.0", | |
| "simple-peer": "^5.11.3", | |
| "socket.io": "^1.3.5", | |
| "socket.io-client": "^1.3.5" | |
| }, | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1", | |
| "start": "node server.js" | |
| }, | |
| "repository": { | |
| "type": "git", | |
| "url": "git+https://gist.github.com/d9bf021c395835427aa0.git" | |
| }, | |
| "license": "MIT", | |
| "homepage": "https://gist.github.com/d9bf021c395835427aa0" | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var _ = require('lodash'); | |
| var http = require('http'); | |
| var browserify = require('browserify-middleware'); | |
| var debug = require('debug')('server'); | |
| var express = require('express'); | |
| var app = express(); | |
| var server = http.Server(app); | |
| var io = require('socket.io')(server); | |
| var DEFAULT_PEER_COUNT = 5; | |
| app.use(express.static(__dirname)); | |
| app.get('/js/bundle.js', browserify(['debug', 'lodash', 'socket.io-client', 'simple-peer', {'./client.js': {run: true}}])); | |
| io.on('connection', function (socket) { | |
| debug('Connection with ID:', socket.id); | |
| var peersToAdvertise = _.chain(io.sockets.connected) | |
| .values() | |
| .without(socket) | |
| .sample(DEFAULT_PEER_COUNT) | |
| .value(); | |
| debug('advertising peers', _.map(peersToAdvertise, 'id')); | |
| peersToAdvertise.forEach(function(socket2) { | |
| debug('Advertising peer %s to %s', socket.id, socket2.id); | |
| socket2.emit('peer', { | |
| peerId: socket.id, | |
| initiator: true | |
| }); | |
| socket.emit('peer', { | |
| peerId: socket2.id, | |
| initiator: false | |
| }); | |
| }); | |
| socket.on('signal', function(data) { | |
| var socket2 = io.sockets.connected[data.peerId]; | |
| if (!socket2) { return; } | |
| debug('Proxying signal from peer %s to %s', socket.id, socket2.id); | |
| socket2.emit('signal', { | |
| signal: data.signal, | |
| peerId: socket.id | |
| }); | |
| }); | |
| }); | |
| server.listen(process.env.PORT || '3000'); | 
can u please tell me that what is the proper to destroy simple-peer peer connection because upon calling peer.destroy() i get cannot signal after peer is destroyed
Sorry, can you explain where the peer connection take place? i don't understand this :(
Added a repository with credit to the author and video support here: https://github.com/hthetiot/simple-peer-test/blob/master/README.md
lodash v4 change: use
sampleSizeinstead ofsamplein server.js
Done https://github.com/hthetiot/simple-peer-test/commit/6285a7affd203c8f89dcf57cdf0bede980eb1898 thx @falvhb
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
lodash v4 change: use
sampleSizeinstead ofsamplein server.js