binary-event-index.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Demo Chat</title>
  6. <link href="bootstrap.css" rel="stylesheet">
  7. <style>
  8. body {
  9. padding:20px;
  10. }
  11. #console {
  12. height: 400px;
  13. overflow: auto;
  14. }
  15. .username-msg {color:orange;}
  16. .connect-msg {color:green;}
  17. .disconnect-msg {color:red;}
  18. .send-msg {color:#888}
  19. </style>
  20. <script src="js/socket.io/socket.io.js"></script>
  21. <script src="js/moment.min.js"></script>
  22. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  23. <script>
  24. var socket = io.connect('http://localhost:9092');
  25. socket.on('connect', function() {
  26. alert('socketio client connected');
  27. });
  28. socket.on('msg', function(file) {
  29. var arrayBuffer = new Uint8Array(file).buffer;
  30. var blob = new Blob([arrayBuffer]);
  31. var imgList = $('ul#img-list');
  32. var li = $('<li/>').appendTo(imgList);
  33. $('<div/>').text(file.name).appendTo(li);
  34. var img = $('<img/>').appendTo(li);
  35. var reader = new FileReader();
  36. reader.onload = (function(aImg) {
  37. return function(e) {
  38. aImg.attr('src', e.target.result);
  39. aImg.attr('width', 150);
  40. };
  41. })(img);
  42. reader.readAsDataURL(blob);
  43. });
  44. $(document).ready(function() {
  45. var fileInput = $('#file-field');
  46. fileInput.bind({
  47. change: function() {
  48. displayFiles(this.files);
  49. }
  50. });
  51. function displayFiles(files) {
  52. $.each(files, function(i, file) {
  53. if (!file.type.match(/image.*/)) {
  54. return true;
  55. }
  56. var reader = new FileReader();
  57. reader.onload = function(e) {
  58. socket.emit('msg', e.target.result);
  59. };
  60. reader.readAsArrayBuffer(file);
  61. });
  62. }
  63. });
  64. </script>
  65. </head>
  66. <body>
  67. Please upload any image file.
  68. <br/>
  69. <br/>
  70. <div>
  71. <input type="file" name="file" id="file-field" />
  72. </div>
  73. <div id="img-container">
  74. <ul id="img-list"></ul>
  75. </div>
  76. </body>
  77. </html>