Last active
May 17, 2018 15:46
-
-
Save probil/5fd6433b0e58b8b1ebd9ee2f7a429369 to your computer and use it in GitHub Desktop.
This file contains 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 express = require('express'); | |
const app = express(); | |
const server = require('http').Server(app); | |
const io = require('socket.io')(server); | |
server.listen(3000); | |
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 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 Vue from 'vue' | |
import VueSocketio from 'vue-socket.io-extended' | |
import io from 'socket.io-client' | |
Vue.use(VueSocketio, io('http://localhost:3000')) | |
const App = { | |
render: () => null, | |
}; | |
new Vue({ | |
el: '#app', | |
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) | |
} | |
}, | |
mounted() { | |
setTimeout(() => this.clickButton('Hi there'), 5000); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment