scan.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Message } from 'element-ui'
  2. var wsc2={
  3. url : 'ws://127.0.0.1:9999',
  4. ws:null,
  5. is_online:false,
  6. timer_ping:null,
  7. timer_close:null,
  8. interval_ping:10000,
  9. interval_close:5000,
  10. init:function(callback){
  11. var self=this;
  12. Message({
  13. type:'success',
  14. message: '扫描程序正在启动,请稍后……'
  15. })
  16. self.ws= new WebSocket(self.url);
  17. self.ws.onopen = function(evt) {
  18. // console.log("Connection open ...");
  19. self.is_onlien=true;
  20. self.ws.send("client connect success");
  21. start_heartbeat();
  22. };
  23. self.ws.onmessage = function(evt) {
  24. console.log("打印扫描仪返回的evt: " +evt.data);
  25. if(evt.data=="ping"){
  26. self.send("pong");
  27. // console.log("on ping");
  28. }
  29. else if(evt.data=="pong"){
  30. // console.log("on pong");
  31. }
  32. else{
  33. // console.log( "Received Message: " + evt.data);
  34. let objectData=JSON.parse(evt.data);
  35. console.log("扫描仪返回的数据",objectData);
  36. if (objectData.Data) {
  37. let obj=JSON.parse(objectData.Data.data_string);
  38. console.log("打印返回的图片地址",obj);
  39. // let obj={
  40. // code:200,
  41. // data:objectData.entity.fi_full_name,
  42. // msg:'图片扫描成功',
  43. // };
  44. // callback(obj);// 通过回调函数返回图片地址
  45. }
  46. if (objectData.code === 509) {
  47. // Message({
  48. // type: 'error',
  49. // message: '服务器错误,请检查日志'
  50. // })
  51. } else if (JSON.parse(evt.data).code === 502) {
  52. Message({
  53. type: 'error',
  54. message: '未检测到纸张或卡纸'
  55. })
  56. self.ws.close();//关闭链接
  57. }
  58. else if (JSON.parse(evt.data).code === 200) {
  59. Message({
  60. type: 'success',
  61. message: '扫描仪启动成功!'
  62. })
  63. }
  64. }
  65. clearTimeout(self.timer_close);
  66. clearTimeout(self.timer_ping);
  67. start_heartbeat();
  68. };
  69. self.ws.onclose = function(evt) {
  70. console.log("连接关闭");
  71. };
  72. self.ws.onerror=function(evt){
  73. console.log(evt);
  74. };
  75. //心跳检测
  76. function start_heartbeat(){
  77. self.timer_ping=setTimeout(function(){
  78. self.send("ping");//发送一次 "ping" 消息给服务器。
  79. //设置关闭定时器,如果超过 self.interval_close 毫秒没有收到服务器的响应,则认为连接已经断开,关闭 WebSocket 连接。
  80. self.timer_close=setTimeout(function(){
  81. self.ws.close();
  82. self.is_online=false;
  83. on_offline();//处理离线逻辑方法
  84. },self.interval_close);
  85. },self.interval_ping);//每隔 self.interval_ping 毫秒
  86. }
  87. //离线处理逻辑
  88. function on_offline(){
  89. }
  90. },
  91. send:function(data){
  92. if (this.ws.readyState === this.ws.OPEN)
  93. {
  94. console.log("发送数据:" + data);
  95. this.ws.send(data);
  96. } else {
  97. Message({
  98. type: 'error',
  99. message: '请确认扫描客户端是否已经启动',
  100. duration: 5000
  101. });
  102. }
  103. // console.log("发送数据:"+data);
  104. // try {
  105. // this.ws.send(data);
  106. // } catch(e) {
  107. // Message({
  108. // type: 'error',
  109. // message: '请确认扫描客户端是否已经启动'
  110. // })
  111. // }
  112. },
  113. }
  114. export default wsc2