I'm using the Vue-cli webpack template to generate my project about a chatroom with socket.io.
I have tested it with a simple instance when I click the button, the client-side will emit the params to the server-side. But the server-side haven't got anything when I clicked the button.
Could anyone tell me what's the matter with my coding? How can I do to solve that?
Thank you very much!
Here is the client-side
<template>
<div id="app">
<input type='button' value='button' @click='clickButton()'>
</div>
</template>
<script>
import Vue from 'vue'
import VueSocketio from 'vue-socket.io';
Vue.use(VueSocketio, 'http://localhost:8070/');
export default {
name: 'app',
data() {
return {
msg: 'Welcome to Your Vue.js App'
}
},
sockets: {
connect: function() {
console.log('socket connected')
},
customEmit: function(val) {
console.log('this method was fired by the socket server. eg:
io.emit("customEmit", data)')
}
},
methods: {
clickButton: function(val) {
// $socket is socket.io-client instance
this.$socket.emit('a', 12, 5);
}
}
}
</script>
Here is the server
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io');
server.listen(8070);
const ws = io.listen(server);
ws.on('connection',(sock)=>{
sock.on('a',(num1,num2)=>{
console.log(`${num1},${num2}`)
})
})