Last active
July 4, 2018 09:53
-
-
Save harishankards/82c8d103381df5c6f79af15bca8392e0 to your computer and use it in GitHub Desktop.
To debug socket.io implementation with node and Vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const app = express(); | |
| var server = require('http').Server(app); | |
| const io = require('socket.io')(server); | |
| io.on('connection', function(socket){ | |
| console.log("---------------------------------------------------------") | |
| console.log('An user connected'); | |
| console.log("---------------------------------------------------------") | |
| socket.on('disconnect', function(){ | |
| console.log('user disconnected'); | |
| }); | |
| socket.on('emit_method', function(msg){ | |
| console.log('message: ' + msg); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const server = app.listen(app.get('port'), () => { | |
| console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env')); | |
| console.log(' Press CTRL-C to stop\n'); | |
| }); | |
| const express = require('express'); | |
| const socket = require('socket.io'); | |
| const app = express(); | |
| const io = socket(server); | |
| io.on('connection', function(socket){ | |
| console.log("---------------------------------------------------------") | |
| console.log('An user connected'); | |
| console.log("---------------------------------------------------------") | |
| socket.on('disconnect', function(){ | |
| console.log('user disconnected'); | |
| }); | |
| socket.on('emit_method', function(msg){ | |
| console.log('message: ' + msg); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import VueSocketio from 'vue-socket.io-extended' | |
| import io from 'socket.io-client' | |
| Vue.use(VueSocketio, io('http://localhost:3000')) | |
| new Vue({ | |
| el: '#app', | |
| router, | |
| store, | |
| template: '<App/>', | |
| components: { App }, | |
| sockets: { | |
| connect () { | |
| console.log('socket connected') | |
| }, | |
| customEmit (val) { | |
| console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)', val) | |
| } | |
| }, | |
| methods: { | |
| clickButton (val) { | |
| // this.$socket is `socket.io-client` instance | |
| this.$socket.emit('emit_method', val) | |
| } | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment