• websocket投送


    一、websocket投送

    a.vue页面实现websocket投送

    1、点击页面投送调用websocket的初始化

    1. <template>
    2. <div class="cssFont" @click="goWebSocket">点击将页面投送div>
    3. template>
    4. <script>
    5. export default {
    6. methods: {
    7. goWebSocket(){
    8. this.initWebSocket(); //调用初始化
    9. },
    10. }
    11. }
    12. script>

    2、methods里写好所有的websocket方法

    1. <template>
    2. <div class="cssFont" @click="goWebSocket">点击将页面投送div>
    3. template>
    4. <script>
    5. export default {
    6. mounted() {
    7. },
    8. beforeDestroy() {
    9. this.websocketOnclose();
    10. },
    11. methods: {
    12. goWebSocket(){
    13. this.initWebSocket(); //调用初始化
    14. },
    15. initWebSocket: function () {
    16. const that = this;
    17. this.loading = true;
    18. that.websocketOnclose();
    19. let projectType = this.CQfrom.projectType; //项目类型
    20. let projectCode = this.CQfrom.projectCode; //项目code
    21. // this.deviceVal 当前对接链接的设备
    22. let url = `${process.env.VUE_APP_WS_API}/websocket/${this.deviceVal}/${projectType}/${projectCode}`;
    23. this.websock = new WebSocket(url); //新建一个WebSocket的构造函数
    24. this.websock.onopen = this.websocketOnopen; //连接成功
    25. this.websock.onerror = this.websocketOnerror; //连接失败
    26. this.websock.onmessage = this.websocketOnmessage; //消息返回
    27. this.websock.onclose = this.websocketOnclose; //关闭websocket
    28. },
    29. websocketOnopen: function () {
    30. console.log("WebSocket连接成功");
    31. //开始
    32. let val = { val:'需要被投送的显示内容1',val2:'需要被投送的显示内容2'};
    33. val["username"] = store.getters.name;
    34. let params = {
    35. projectCode: val.projectCode,
    36. projectType: val.projectType,
    37. routeUrl: "/pgd/patientForm/deliveryBook",
    38. client: this.deviceVal,
    39. paramsJsonStr: JSON.stringify(val),//此处就是我传递需要渲染到签字页面上的数据,
    40. filePath: null,
    41. fileView: null,
    42. };
    43. if (this.CQfrom.projectId) {
    44. params["projectId"] = this.CQfrom.projectId;
    45. }
    46. this.loading = true;
    47. agreePush(params) //连接成功后调用的函数
    48. .then((res) => {
    49. this.loading = false;
    50. if (res.code === 200) {
    51. this.$message({
    52. message: `推送成功到设备${this.deviceVal}`,
    53. type: "success",
    54. });
    55. window.localStorage.setItem(`deviceVal`, this.deviceVal);
    56. }
    57. })
    58. .catch((err) => {
    59. this.loading = false;
    60. });
    61. //结束
    62. },
    63. websocketOnerror: function (e) {
    64. console.log("WebSocket连接发生错误");
    65. this.initWebSocket(); //连接发生错误重新连接
    66. },
    67. websocketOnmessage: function (e) {
    68. console.log("-----接收消息-------", e);
    69. if (JSON.parse(e.data)) {
    70. //接受返回的消息,如果接受到已经签字提交完成后,调用关闭websock接口,直接关闭
    71. //开始
    72. window.sessionStorage.setItem("pdfData", e.data);
    73. this.websock.onclose();
    74. setTimeout(() => {
    75. this.$router.go(-1);
    76. }, 100);
    77. //结束
    78. }
    79. console.log("-----接收消息-------", JSON.parse(e.data));
    80. },
    81. websocketOnclose: function (e) {//关闭websocket的接口
    82. if (e) {
    83. console.log("connection closed (" + JSON.parse(e.code) + ")");
    84. }
    85. },
    86. },
    87. };
    88. script>
    89. <style rel="stylesheet/scss" lang="scss" scoped>
    90. .cssFont {
    91. font-size: 16px;
    92. color: #363e45;
    93. font-weight: 600;
    94. padding: 0 60px 0 60px;
    95. height: 46px;
    96. line-height: 46px;
    97. font-family: PingFangSC, PingFang SC;
    98. }
    99. style>

    注意:后面就是等着签字端签完然后将签完的内容投送回来 

  • 相关阅读:
    彻底解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)
    EMQX的docker部署与使用(mqtt)
    【网络安全】学过编程就是黑客?
    产品新闻稿撰写流程是怎样的,纯干货
    java计算机毕业设计流浪狗领养系统源码+系统+mysql数据库+lw文档+部署
    操作系统学习案例20240222-liunx自定义systemctl系统服务
    麒麟系统开发笔记(七):在线安装软件后,提取其安装包,部署目标机使用离线软件包方式安装软件
    LCR 068.搜索插入位置
    Java版企业电子招标采购系统源码—企业战略布局下的采购寻源
    通过AWS S3快速低成本部署静态网站,九河云一文详细教程
  • 原文地址:https://blog.csdn.net/Seven_Ting/article/details/140929514