Middleware.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var webSocket = undefined
  2. import Common from "../common/Common.js";
  3. import Notify from "pc-component-v3").Notify;
  4. export default {
  5. //打开websocket
  6. open: function (readerEnvent) {
  7. if (this.webSocket == null) {
  8. //webSocket的URL
  9. var url = Common.getMidURL("/ws/ReaderEventWebSocket");
  10. url = websocketUrl.replace("https","wss").replace("http", "ws");
  11. //创建新的webSocket
  12. this.webSocket = new WebSocket(url);
  13. }else{
  14. Notify.info("提示", "webSocket已经连接", false);
  15. }
  16. this.webSocket.onmessage = function (evt) {
  17. var datas = JSON.parse(evt.data);
  18. if (datas != null) {
  19. readerEnvent(datas);
  20. }
  21. };
  22. this.webSocket.onopen = function () {
  23. Notify.success("提示", "webSocket已连接",1500);
  24. }
  25. this.webSocket.onerror = function (evt) {
  26. Notify.error("提示", "webSocket出错", false);
  27. };
  28. this.webSocket.onclose = function (evt) {
  29. //Notify.info("提示", "webSocket已断开", false);
  30. };
  31. },
  32. //关闭websocket
  33. close: function () {
  34. if (this.webSocket) {
  35. this.webSocket.close()
  36. this.webSocket = null;
  37. };
  38. },
  39. //订阅读写器
  40. subscribe: function (readerId) {
  41. this.webSocket.send("Subscribe " + readerId);
  42. },
  43. //取消订阅
  44. unSubscribe: function (readerId) {
  45. this.webSocket.send("UnSubscribe " + readerId);
  46. },
  47. //启动盘点
  48. start: function (readerId) {
  49. $.ajax({
  50. type: "GET",
  51. url: Common.getMidURL("/api/ReaderResource/startInventory/" + readerId),
  52. dataType: "json",
  53. contentType: "application/json",
  54. success: function (result) {
  55. Notify.info("提示", "开启盘点成功", false);
  56. },
  57. error: function (XMLHttpRequest, textStatus, errorThrown) {
  58. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  59. }
  60. });
  61. },
  62. //关闭盘点
  63. stop: function (readerId) {
  64. $.ajax({
  65. type: "GET",
  66. url: Common.getMidURL("/api/ReaderResource/stopInventory/" + readerId),
  67. dataType: "json",
  68. contentType: "application/json",
  69. success: function (statusConfigs) {
  70. Notify.info("提示", "关闭盘点成功", false);
  71. },
  72. error: function (XMLHttpRequest, textStatus, errorThrown) {
  73. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  74. }
  75. });
  76. }
  77. }