1
0

index.js 3.0 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 valid = true;
  11. var file_to_send = document.getElementById("file_to_send").files[0];
  12. var dest_id = document.getElementById("dest_id").value;
  13. if(!file_to_send) {
  14. valid = false;
  15. alert("Please select a file.");
  16. }
  17. if(!dest_id) {
  18. valid = false;
  19. alert("Please provide a Destination ID.");
  20. }
  21. else if (dest_id === peer.id) {
  22. valid = false;
  23. alert("You can't send a file to yourself!");
  24. }
  25. if(!valid){
  26. return;
  27. }
  28. // Build blob from file and file type
  29. var blob = new Blob([file_to_send], {type: file_to_send.type});
  30. // Connect to the desired peer
  31. var con = peer.connect(dest_id, { label: 'file', reliable: true});
  32. con.on('open', function() {
  33. // Send arr of blob and file data
  34. con.send({
  35. name: document.getElementById("file_to_send").files[0].name,
  36. file: blob,
  37. type: document.getElementById("file_to_send").files[0].type
  38. });
  39. });
  40. }
  41. // Display ID
  42. peer.on('open', function(id) {
  43. document.getElementById("my_id").textContent = peer.id;
  44. });
  45. // Constantly listen for incoming connections
  46. peer.on('connection', function(conn) {
  47. conn.on('open', function() {
  48. conn.on('data', function(data) {
  49. var blob = new Blob([data.file], {type: data.type});
  50. var url = window.URL.createObjectURL(blob);
  51. var table = document.getElementById("file_table");
  52. // Remove the "No Files" row
  53. var elem = document.getElementById("empty_row");
  54. if(elem) {
  55. elem.parentNode.removeChild(elem);
  56. //var row = table.insertRow(1);
  57. //row.innerHTML = "File";
  58. //var cell = row.insertCell(0);
  59. //cell.innerHTML = "Time received";
  60. //cell = row.insertCell(0);
  61. //cell.innerHTML = "Source ID";
  62. //cell = row.insertCell(0);
  63. //cell.innerHTML = "Size";
  64. }
  65. var row = table.insertRow(1);
  66. var a = document.createElement("a");
  67. a.href = url;
  68. a.innerHTML = data.name;
  69. a.setAttribute("download", data.name);
  70. var file_col = row.insertCell(0);
  71. file_col.appendChild(a);
  72. //int id_col = row.insertCell(0);
  73. //var d = new Date();
  74. //id_col.innerHTML = d.getHours+':'+d.getMinutes();
  75. });
  76. });
  77. });
  78. peer.on('error', function(err) {
  79. if (err.type === 'peer-unavailable') {
  80. alert("No user with the given ID is currently connected.");
  81. }
  82. });