1、先创建一个js文件
文件内容:export default { ws:{}, setWs: function(newWs){ this.ws = newWs } }
 
2、main.js 文件引入
import websocket from ‘./utils/websocket’
 const app = createApp(App)
 app.config.globalProperties.$websocket = websocket
3、 在需要连接websocket 页面
setup里边声明
 const { proxy } = getCurrentInstance() as any;
onMounted(() => {
       proxy.$websocket.ws = new WebSocket("ws://10.20.10.68:8888/接口地址");    填写通讯地址
       
       proxy.$websocket.setWs(proxy.$websocket.ws);
        proxy.$websocket.ws.onopen = () => {   // 连接地址
           
            console.log("连接成功");
            proxy.$websocket.ws.send(msg);   // send向服务器发送消息
  };
	       proxy.$websocket.ws.onerror = (e: any) => {
	        console.log("连接错误信息", e);
	      };
	      proxy.$websocket.ws.onclose = () => {
	        console.log("断开重连");
	        setTimeout(function() {
	          proxy.$websocket.ws.onopen();
	        }, 1000);
	      };
});










