把socket放到最外面

This commit is contained in:
kron
2025-06-05 21:32:15 +08:00
parent 219fdc54ad
commit 58bd5d9bb2
6 changed files with 110 additions and 73 deletions

View File

@@ -1,7 +1,26 @@
<script setup>
import { onLaunch } from "@dcloudio/uni-app";
import { watch } from "vue";
import websocket from "@/websocket";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
onLaunch(() => {});
watch(
() => user.value.id,
(newVal) => {
const token = uni.getStorageSync("token");
if (newVal && token) {
websocket.createWebSocket(token, (content) => {
uni.$emit("socket-inbox", content);
});
}
},
{
deep: false, // 如果 user 是一个对象或数组,建议开启
immediate: false, // 若想在初始化时立即执行一次回调,可开启。
}
);
</script>
<style>