index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Check for the various File API support.
  2. if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
  3. alert('This website requires HTML5, which your browser does not fully support. Please use a supported browser to use this website.');
  4. }
  5. // Grab a peer ID
  6. var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
  7. // Send a file
  8. function send() {
  9. // Check that all fields are filled
  10. var file_to_send = document.getElementById("file_to_send").files[0];
  11. var dest_id = document.getElementById("dest_id").value;
  12. if(!file_to_send && !dest_id) {
  13. alert("Please select a file and previde a Destination ID.");
  14. }
  15. else if(!file_to_send) {
  16. alert("Please select a file.");
  17. }
  18. else if(!dest_id) {
  19. alert("Please provide a Destination ID.");
  20. }
  21. else if (dest_id === peer.id) {
  22. alert("You can't send a file to yourself!");
  23. }
  24. else {
  25. // Build blob from file and file type
  26. var blob = new Blob([file_to_send], {type: file_to_send.type});
  27. // Connect to the desired peer
  28. var con = peer.connect(dest_id, { label: 'file', reliable: true});
  29. con.on('open', function() {
  30. // Send arr of blob and file data
  31. con.send({
  32. name: document.getElementById("file_to_send").files[0].name,
  33. file: blob,
  34. type: document.getElementById("file_to_send").files[0].type
  35. });
  36. });
  37. }
  38. }
  39. // Display ID
  40. peer.on('open', function(id) {
  41. document.getElementById("my_id").textContent = peer.id;
  42. });
  43. // Constantly listen for incoming connections
  44. peer.on('connection', function(conn) {
  45. conn.on('open', function() {
  46. conn.on('data', function(data) {
  47. // TODO consider sending a size blob beforehand, or a size field in the blob
  48. // A good starting point would be to continuously update the entry with how
  49. // much data was downloaded
  50. var blob = new Blob([data.file], {type: data.type});
  51. var url = window.URL.createObjectURL(blob);
  52. var table = document.getElementById("file_table");
  53. // Remove the "No Files" row
  54. var elem = document.getElementById("empty_row");
  55. if(elem) {
  56. elem.parentNode.removeChild(elem);
  57. //var row = table.insertRow(1);
  58. //row.innerHTML = "File";
  59. //var cell = row.insertCell(0);
  60. //cell.innerHTML = "Time received";
  61. //cell = row.insertCell(0);
  62. //cell.innerHTML = "Source ID";
  63. //cell = row.insertCell(0);
  64. //cell.innerHTML = "Size";
  65. }
  66. var row = table.insertRow(1);
  67. var a = document.createElement("a");
  68. a.href = url;
  69. a.innerHTML = data.name;
  70. a.setAttribute("download", data.name);
  71. var file_col = row.insertCell(0);
  72. file_col.appendChild(a);
  73. //int id_col = row.insertCell(0);
  74. //var d = new Date();
  75. //id_col.innerHTML = d.getHours+':'+d.getMinutes();
  76. });
  77. });
  78. });
  79. peer.on('error', function(err) {
  80. if (err.type === 'peer-unavailable') {
  81. alert("No user with the given ID is currently connected.");
  82. }
  83. });