Tag:vue.jsjavascript
Article From:https://segmentfault.com/q/1010000012146143
Question:

The use of websocket in a project
After the Login.vue login is completed, the websocket server is successfully connected to the server by using the returned parameters.
The websocket object is obtained

How do I use this websocket object in other Vue modules? That is to say, share a long connection object between Vue modules?

Answer 0:

A file called ws.js

const ws
function getWs(options) {
  if (!ws) {
    ws = new WebSocket(options)
  }
  return ws
}
export default getWs

Where to use is used.

Answer 1:

When you need it, then new a new websocket object, or use vuex to see it.

Leave a Reply

Your email address will not be published. Required fields are marked *