• Vue封装websocket双向通讯


    1. 封装的socket.js文件内容:

    1. var websock = null;
    2. var global_callback = null;
    3. var serverPort = '80'; // webSocket连接端口
    4. var wsuri = 'wss://uat.sssyin.cn/ws-reservation';
    5. function createWebSocket(callback) {
    6. if (websock == null || typeof websock !== WebSocket) {
    7. initWebSocket(callback);
    8. }
    9. }
    10. function initWebSocket(callback) {
    11. global_callback = callback;
    12. // 初始化websocket
    13. websock = new WebSocket(wsuri);
    14. websock.onmessage = function (e) {
    15. websocketonmessage(e);
    16. };
    17. websock.onclose = function (e) {
    18. websocketclose(e);
    19. };
    20. websock.onopen = function () {
    21. websocketOpen();
    22. };
    23. // 连接发生错误的回调方法
    24. websock.onerror = function () {
    25. console.log('WebSocket连接发生错误');
    26. //createWebSocket();啊,发现这样写会创建多个连接,加延时也不行
    27. };
    28. }
    29. // 实际调用的方法
    30. function sendSock(agentData ) {
    31. if (websock.readyState === websock.OPEN) {
    32. // 若是ws开启状态
    33. websocketsend(agentData);
    34. } else if (websock.readyState === websock.CONNECTING) {
    35. // 若是 正在开启状态,则等待1s后重新调用
    36. setTimeout(function () {
    37. sendSock(agentData);
    38. }, 1000);
    39. } else {
    40. // 若未开启 ,则等待1s后重新调用
    41. setTimeout(function () {
    42. sendSock(agentData);
    43. }, 1000);
    44. }
    45. }
    46. function closeSock() {
    47. websock.close();
    48. }
    49. // 数据接收
    50. function websocketonmessage(msg) {
    51. // console.log("收到数据:"+JSON.parse(e.data));
    52. // console.log("收到数据:"+msg);
    53. // global_callback(JSON.parse(msg.data));
    54. // 收到信息为Blob类型时
    55. let result = null;
    56. // debugger
    57. if (msg.data instanceof Blob) {
    58. const reader = new FileReader();
    59. reader.readAsText(msg.data, 'UTF-8');
    60. reader.onload = e => {
    61. result = JSON.parse(reader.result);
    62. //console.log("websocket收到", result);
    63. global_callback(result);
    64. };
    65. } else {
    66. result = JSON.parse(msg.data);
    67. //console.log("websocket收到", result);
    68. global_callback(result);
    69. }
    70. }
    71. // 数据发送
    72. function websocketsend(agentData) {
    73. console.log(`发送数据:${ agentData }`);
    74. websock.send(agentData);
    75. }
    76. // 关闭
    77. function websocketclose(e) {
    78. console.log(`connection closed (${ e.code })`);
    79. }
    80. function websocketOpen(e) {
    81. console.log('连接打开');
    82. }
    83. export { sendSock, createWebSocket, closeSock };

    2、页面调用

    1. <template>
    2. <div>
    3. <button>发消息</button>
    4. </div>
    5. </template>
    6. <script>
    7. import { sendSock, createWebSocket, closeSock } from './sockt.js';
    8. export default {
    9. data () {
    10. return {
    11. }
    12. },
    13. created() {
    14. this.init();
    15. },
    16. destroyed(){
    17. closeSock();
    18. },
    19. methods: {
    20. init() {
    21. createWebSocket(this.global_callback);
    22. },
    23. send(){
    24. var sendData = {
    25. operate:'singleChannelSwitch',
    26. index:index+1,
    27. opera:row.button_relay
    28. };
    29. sendSock(1111);
    30. },
    31. // websocket的回调函数,msg表示收到的消息
    32. global_callback(msg) {
    33. console.log(`websocket的回调函数收到服务器信息:${ JSON.stringify(msg) }`);
    34. // console.log("收到服务器信息:" + msg);
    35. },
    36. }
    37. }
    38. </script>
    39. <style>
    40. </style>

  • 相关阅读:
    spring redis工具类
    MATLAB小技巧(23)矩阵分析--模拟退火
    如果发现薅来的封装是错误的,请不要怪羊
    Guava的EventBus
    小白学python系列————【Day50】选课系统项目
    Qt使用共享内存实现进程通信
    苍穹外卖项目
    972信息检索 | 第四章 国内重要的综合性信息检索系统
    网络安全-抓包和IP包头分析
    C#(二) C#高级进阶
  • 原文地址:https://blog.csdn.net/yanby921005/article/details/132899311