11
heimawl
2023-07-04 a8799531bd3918b2416b95914b02101ce1337ddb
src/views/shortmessage/communication/index.vue
@@ -65,7 +65,11 @@
<script>
import LemonMessageVoice from "./lemon-message-voice";
import EmojiData from "./database/emoji";
import { sendWebsocket, closeWebsocket } from "@/websocket/websocket.js";
import {
  sendWebsocket,
  websocketSend,
  closeWebsocket,
} from "@/websocket/websocket.js";
// Vue.component(LemonMessageVoice.name, LemonMessageVoice);
// 获取时间
const getTime = () => {
@@ -581,19 +585,66 @@
      instance.closeDrawer();
    },
    // 建立连接
    requstWs() {
    requstWs(message, next, file) {
      console.log("连接");
      // 防止用户多次连续点击发起请求,所以要先关闭上次的ws请求。
      closeWebsocket();
      // closeWebsocket();
      if (file) {
        const reader = new FileReader();
        console.log(reader);
        console.log(file);
        reader.onload = (file) => {
          console.log(file);
          console.log(reader);
          const imageData = reader.result;
          //文件字节大小
          //  const fileSize = file.loaded;
          const fileSize = imageData.length;
          console.log("byteLength : ", fileSize);
          const chunkSize = 4096; // 设置每个数据块的大小
          let offset = 0;
          // 分块传输图片数据
          while (offset < imageData.length) {
            const chunk = imageData.slice(offset, offset + chunkSize);
            const obj = {
              type: file.type,
              userId: "1", //接收数据方
              fileSize: fileSize,
              image: chunk,
            };
            console.log(JSON.stringify(obj));
            // 发起ws请求
            // sendWebsocket(
            //   "ws://192.168.31.121:8080/chat?userId=2",
            //   JSON.stringify(obj),
            //   this.wsMessage,
            //   this.wsError
            // );
            websocketSend(JSON.stringify(obj));
            offset += chunkSize;
            console.log("offset : ", offset);
          }
          // 发送完成标记
          console.log("message ");
        };
        return reader.readAsDataURL(file);
      }
      // 跟后端协商,需要什么参数数据给后台
      const obj = {
        type: "text",
        content: "帝得圣相相曰度",
        type: "object",
        userId: "1", //接收数据方
        fileSize: "1212",
        image: "12",
      };
      // 发起ws请求
      sendWebsocket(
        "ws://192.168.1.4:8080/chat?userId=2",
        "ws://192.168.31.121:8080/chat?userId=2",
        obj,
        this.wsMessage,
        this.wsError
@@ -628,7 +679,7 @@
      console.log(message);
      console.log(file);
      console.log(next);
      this.requstWs();
      this.requstWs(message, next, file);
      setTimeout(() => {
        next();
      }, 1000);