diff --git a/composables/useSocket.ts b/composables/useSocket.ts new file mode 100644 index 0000000..d600ec6 --- /dev/null +++ b/composables/useSocket.ts @@ -0,0 +1,22 @@ +import useUserInfo from "~/composables/useUserInfo"; + +export default function useSocket(){ + return new Promise(()=>{ + const user = useUserInfo() + // 连接服务端 + const socket = io('http://research.mcnetmart.com:2120');//这里请填写你的域名,外网,端口为socket端口 + + socket.on('connect', function () { + this.emit('login', user.$state.info.id); + }); + + // 后端推送来消息时 + socket.on('translateDoc', async function (msg) {//这里的new_msg请一定要注意,官方文档都写的是content,但是后端发送的自定义是new_msg,后端定义成new_msg,前端却接受content的字段。所以是接受不了的 + console.log("收到消息:" + msg); + // 调接口 + + resolve(接口结果) + // await fetch('http://research.mcnetmart.com/api/translate/getUserTranslateDocInfo?id='+msg,{headers:{token: 'cd91b1e5-0ba0-4c47-ad17-48d6dfa63224'}}) + }); + }) +}