this simple experiment vue-socket.io.
express used serve index.html
locally.
the sockets being handled http://metinseylan.com:1923
.
i've defined custom socket inside of main.js
named testclicked
. test button bound via vue.js clickbutton()
method. inside of clickbutton()
2 emit calls:
this.$socket.emit('connect', val); // works this.$socket.emit('testclicked', val); // fails
i not understand why first 1 works, second 1 fails. put console output @ bottom.
i tried adding testclicked
var methods = [...];
inside of vue-socketio.js, no avail.
index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>vue-socket-dynamo</title> </head> <body id="vue-socket-dynamo"> <button @click="clickbutton('testing 123')">test</button> <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="vue.js"></script> <script src="vue-socketio.js"></script> <script src="main.js"></script> </body> </html>
vue-socketio.js copy , pasted here
main.js
var metin = 'http://metinseylan.com:1923'; vue.use(vuesocketio, metin); // automatically socket connect url string var vm = new vue({ el: '#vue-socket-dynamo', sockets:{ connect: function(val){ if(val) { console.log('socket connected -> val: ', val); } else { console.log('socket connected'); } }, testclicked: function(val){ console.log('testclicked method fired socket server. eg: io.emit("customemit", data)'); if(val) { console.log('val: ', val); } } }, methods: { clickbutton: function(val){ // $socket socket.io-client instance console.log('@click=clickbutton triggered'); // works. console.log('input val: ', val); this.$socket.emit('connect', val); // works this.$socket.emit('testclicked', val); // not working } } });
have coded emit method on server side?
mysocketio/index.js module.exports = { init: function (server) { var io = require('socket.io') var listeningio = io.listen(server, null) var listeningiochat = listeningio.of('/chat') listeningiochat.on('connection', function (socket) { console.log('a user connected chat') socket.on('testclicked', function (msg) { console.log("testclicked") listeningiochat.emit('testclicked', msg); // line trigger vuesocketio event }); socket.on('disconnect', function () { console.log('user disconnected'); }); }); } }
Comments
Post a Comment