daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Sep 10th, 2017 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <head>
  3.   <meta charset="utf-8">
  4.   <title>Image Scanner</title>
  5.   <script type="text/javascript" src="./jquery.min.js"></script>
  6. </head>
  7. <body>
  8.   <h1>Pemindai Gambar</h1>
  9.   <p>
  10.     Klik tombol berikut untuk mulai pemindaian.
  11.   </p>
  12.   <p>
  13.     <button id="btnScan" onclick="scan();">Pindai</button>
  14.   </p>
  15.   <p>
  16.     <img id="imgScanResult" style="margin: auto;" />
  17.   </p>
  18.   <script type="text/javascript">
  19.     var ws_available = false;
  20.     var socket = null;
  21.  
  22.     if (window.WebSocket) {
  23.       console.log('WebSocket is supported by your browser.');
  24.       ws_available = true;
  25.     }
  26.     else {
  27.       console.log('Websocket is not supported by your browser.');
  28.       $('#btnScan').attr('disabled', 'disabled');
  29.       ws_available = false;
  30.     }
  31.  
  32.     function sendcmd(cmd) {
  33.       if (socket != null) {
  34.         socket.send(cmd);
  35.       }
  36.     };
  37.  
  38.     function closews() {
  39.       socket.close();
  40.     }
  41.  
  42.     function openws(cmd) {
  43.       if (socket != null) closews();
  44.       if (ws_available) {
  45.         var serviceUrl = 'ws://localhost:8811/';
  46.         socket = new WebSocket(serviceUrl);
  47.         socket.binaryType = "blob";
  48.  
  49.         socket.onopen = function () {
  50.           console.log('Connection established!');
  51.           switch (cmd) {
  52.             case 'scan':
  53.               console.log('Scanning image...');
  54.               break;
  55.           }
  56.           sendcmd(cmd);
  57.         };
  58.  
  59.         socket.onclose = function () {
  60.           console.log('Connection closed!');
  61.         };
  62.  
  63.         socket.onerror = function (error) {
  64.           console.log('Error occured: ', error);
  65.         };
  66.  
  67.         socket.onmessage = function (e) {
  68.           if (typeof e.data === "string") {
  69.             console.log(e.data);
  70.           }
  71.           else if (e.data instanceof ArrayBuffer) {
  72.             console.log('ArrayBuffer received: ' + e.data);
  73.           }
  74.           else if (e.data instanceof Blob) {
  75.             console.log('Blob received: ' + e.data.bytes + ' bytes');
  76.           }
  77.           switch (cmd) {
  78.             case 'scan':
  79.               $('#imgScanResult').attr('src', e.data);
  80.               break;
  81.             default:
  82.               console.log('Unknown command');
  83.           }
  84.           closews();
  85.           socket = null;
  86.         };
  87.       }
  88.     }
  89.  
  90.     function scan() {
  91.       openws('scan');
  92.     }
  93.  
  94.   </script>
  95.  
  96. </body>
RAW Paste Data
Top