I have made a chat app with vue-cli for practice. I used vue-socket.io-extended as the plugin on client side for making socket-io connections.
I emit to server in vuex store so that I am able to connect to the socket inside any commonments.
Here is how I write the emit call in vuex actions:
actions: {
emitSendMsg(dispatch, msgToSend){
return new Promise((resolve, reject) => {
this._vm.$socket.client.emit('msg', msgToSend,(error)=>{
if(error){
return reject(error);
}
resolve();
})
})
}
}
and this is how I call the emit function in vue components methods:
methods:{
sendMsg(){
const self = this;
if(!self.msgToSend){
return alert('Please input mssage');
}
self.disableCompose();
self.$store.dispatch("emitSendMsg", self.msgToSend).then(() => {
self.clearCompose();
}).catch(function(error) {
alert(error);
self.clearCompose();
});
}
}
As you can see, I used Promise so that the methods can do call-back actions.
Actually it works fine, the problem is, in the emit functions in vuex, I created a variable "dispatch" and I haven't actually used it, and I make me don't pass the rule "no-unused-vars" of es-lint and I can not commit my file to git unless I disable the rules.
I've tried the syntax below as the plug-in's npm page provided but it doesn't work.
actions: {
emitSocketEvent(data) {
this._vm.$socket.client.emit('eventName', data);
this._vm.$socket.client.emit('with-binary', 1, '2', { 3: '4', 5: new Buffer(6) });
}
}
So is there any more proper way to do the job?