4

I'm creating a system that enables users to share files accross devices connected to the same wifi network. I would like to have a list of all connected devices so I can select which device to send to (Using simplepeer JS).

I have already created a small program that can share files using simplepeer JS

<p>Your id</p>
<div>
  <textarea id="yourId"></textarea>
</div>
<div><button id="connect_bt">Connect</button></div>

<div style="margin-bottom:20px;">
  <p>Other id</p>
  <div>
    <textarea id="otherId"></textarea>
  </div>
  <div></div>
</div>

<div>
  <textarea id="message_input"></textarea>
  <button id="send_message">Send message</button>
</div>

<pre id="messages"></pre>
<script src="simplepeer/simplepeer.min.js"></script>
var peer = new SimplePeer({
    initiator: location.hash === '#init',
    trickle: false
});

peer.on('signal', function(data) {
    document.getElementById('yourId').value = JSON.stringify(data);
});

document.getElementById('connect_bt')
        .addEventListener('click', function() {
            var other_id = JSON.parse(document.getElementById('otherId').value);
            peer.signal(other_id);
        });

document.getElementById('send_message')
        .addEventListener('click', function() {
            var message = document.getElementById('message_input').value;
            peer.send(message);
        });

peer.on('data', function(data) {
    document.getElementById('messages').textContent += data + '\n';
});

if (peer) {
    //list all devices connected
    var devices_list = //something that returns all devices connected to the same wifi network
    console.log(devices_list)
}
barbsan
  • 3,238
  • 11
  • 18
  • 27
phi lo czar
  • 198
  • 11
  • It is not possible to obtain the devices over the network using only WebRTC. To achieve this, you will need to use the backend and register every available device firstly. – Mariusz Beltowski Jun 17 '19 at 15:48
  • Hey Mariusz, Am an excellent php developer. Do you have a hint of how i can register each device other than using ip address? – phi lo czar Jun 17 '19 at 16:57
  • You can use WebRTC to detect when clients are on the same network, which isn't exactly the same, but should get you started, if I understand your use-case correctly. E.g. https://www.sharedrop.io/ does this. – jib Jun 17 '19 at 17:44
  • Hi jib, i want to list all devices connected to a common wifi network so that i can select which device the files should go to. Assuming there are more than two devices connected, i should be able to select the device to send files to. – phi lo czar Jun 17 '19 at 18:10
  • How is that device going to receive the file? Sounds like you'll need some JS on the receiving end as well, hence my suggestion. – jib Jun 17 '19 at 23:29
  • Yeah jib was right, i want to view devices just like sharedrop.io does. I will then handle the file transfer myself. – phi lo czar Jun 18 '19 at 05:59

0 Answers0