4月 10, 2020

透過 Websocket 讀取健保卡

1. 安裝後端元件

2. 讀卡指令(Websocket)
$(document).ready(function(){ connectws(); });
function connectws()
{
  wsUri = "wss://iccert.nhi.gov.tw:7777/echo";  /* Backend */
  websocket = new WebSocket(wsUri);
  websocket.onopen = function(evt) { onOpen(evt) };
  websocket.onclose = function(evt) { onClose(evt) };
  websocket.onmessage = function(evt) { onMessage(evt) };
  websocket.onerror = function(evt) { onError(evt) };
}
function ws_GetBasic() { $("#name").html(""); websocket.send("GetBasic"); }   
function onOpen(evt) { $("#state").html("健保卡元件/正常"); }
function onClose(evt) { $("#state").html("Not connected"); }
function onMessage(evt) 
{ 
  var message = evt.data;
  if (message.substring(0, 9) == 'GetBasic:')
  {
    var res = message.split(",", 5);  
    // Data in res[1] ~ res[3]
  }
}
function onError(evt) { $("#state").html("健保卡元件/失敗" + evt.data); }

沒有留言:

張貼留言