SpringBoot项目中渐进式引入Vue

兄弟姐妹们,我请教一个问题:就是我在方法initWebSocket中成功的创建了一个WebSocket对象,但是我想在下面的sendMessageToTargetUser方法中想去使用这个WebSocket对象,该怎么做?

methods:{
            //初始化WebSocket连接
            initWebSocket(){
                let _this = this
                var host = window.location.host;
                //获取域名:localhost:8080
                console.log("获取的域名为:"+host)
                let webSocket = new WebSocket("ws://"+host+"/chat/"+this.user.userName);
                console.log("这个WebSocket连接是什么?")
                console.log(webSocket);
                console.log(this)
                //给websocket连接绑定事件
                webSocket.onopen = function () {
                    //成功建立websocket连接之后需要做什么事
                }
                //客户端接收服务端推送的消息
                webSocket.onmessage = function (event) {
                    var dataStr = event.data;
                    var obj = JSON.parse(dataStr);
                    console.log("广播消息为:"+obj.message)
                    console.log(dataStr);
                    //判断接收的消息是否为系统消息
                    if (obj.isSystem){
                        //1、展示当前在线用户
                        _this.onLineUser = obj.toUserName
                        //2、发送广播消息
                        console.log(obj.message)
                        _this.broadcastMessage.unshift(obj.message)
                    }else {

                    }
                }
            },
           //客户端向服务端发送消息
           sendMessageToTargetUser() {}
        }

将websocket实例挂载到this上,获者挂载到组件data上,或者挂载到一个全局变量上,这样methods下面哪里都能用