Why can't I use Stomp? when I register Stomp (websocket) on the prototype chain of vue

main.js:

const socket = new SockJS("http://192.168.1.107:8088/webSocketServer");
Vue.prototype.$stompClient = Stomp.over(socket)

index.vue:

connect() {
    const token = this.$Cookies.get("token");
    this.$stompClient.connect({"token": token}, (frame) => {
      console.log("Connected: " + frame);
      this.$stompClient.subscribe("/user/queue/notifications", function (greeting) {
        console.log(JSON.parse(greeting.body));
      });
    });
  },
    

console:

Opening Web Socket...    

has been unable to connect, but when I restart the server once, I can connect successfully once, and then I have been unable to connect, so I have to restart the server

but using it directly in index.vue is no problem

.
 connect() {
    const socket = new SockJS("http://192.168.1.107:8088/webSocketServer");
    this.stompClient = Stomp.over(socket);
    const token = this.$Cookies.get("token");
    this.stompClient.connect({"token": token}, (frame) => {
      console.log("Connected: " + frame);
      this.stompClient.subscribe("/user/queue/notifications", function (greeting) {
        console.log(JSON.parse(greeting.body));
      });
    });
  },
  
The difference between the top and bottom of

is that one registers Stomp on the prototype, one is created in the page method, and I don"t understand why there is an error, a normal,

.

after registering on the prototype, you can only succeed by restarting the server. After that, all connections are Opening Web Socket..

I can"t even change my browser


< del > I suspect it's the ws server < / del >
look at it and think it's not. It should be the problem mentioned above
your problem description is too misleading
you can try it, hang it on the prototype chain, instead of restarting the server, restart the browser, and every time you connect successfully for the first time


part of the source code:

over: function(ws) {
  return new Client(ws);
};
The new Client, inside the

function returns a new object, and Stomp.over (socket) is a new object once executed;

var obj = new Object({a:1});
var obj1 = new Object({a:1});
obj==obj1 //false
this.stompClient = Stomp.over(socket);//

this.xx = this.$stompClient
Menu