Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect function dos not trigger #332

Open
nick-bai opened this issue May 18, 2022 · 1 comment
Open

connect function dos not trigger #332

nick-bai opened this issue May 18, 2022 · 1 comment

Comments

@nick-bai
Copy link

nick-bai commented May 18, 2022

my code like this
main.js

import VueSocketIO from 'vue-socket.io'
import config from '@/config/index'

Vue.use(
  new VueSocketIO({
    debug: false,
    connection: config.API_URL + ':' + config.SOCKET_PORT
  })
)

chat.vue

sockets: {
    connect() {
      this.$message.success('connect success')
    },
}

I want make sure that the socket is connected and then to register user. but when i refresh web , sometimes the connect function works and sometimes it dos not work. when I use chrome and open debug(F12), it works every time.
I use the newest version 3.0.10
I try to use old version 3.0.5 and 3.0.7 it still dos not work.

@ghandhikus
Copy link

ghandhikus commented Jul 18, 2022

It's a race condition. If a component is not mounted before the socket connects, it won't trigger connect. If the socket connects after it's mounted it will trigger the events. The same goes for every event like reconnect or disconnect. I do believe the events should trigger but there is a workaround.

sockets: {
  connect: function () {
    this.connected();
  },
  disconnect: function () {
    this.disconnected();
  },
},

mounted() {
  // this.connect is not available until after it's mounted, due to how Vue-Socket.io is written
  if(this.$socket && this.$socket.connected) {
    this.connected();
  } else {
    this.disconnected();
  }
},

methods: {
  connected: function () {
    // do something here
  },

  disconnected: function () {
    // do something here
  },
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants