mirror of
https://github.com/MetinSeylan/Vue-Socket.io.git
synced 2025-04-16 15:21:28 +02:00
2.1.0
This commit is contained in:
parent
c7491fc762
commit
8e0100be09
8 changed files with 143 additions and 86 deletions
90
README.md
Normal file → Executable file
90
README.md
Normal file → Executable file
|
@ -1,33 +1,39 @@
|
|||
# Vue-Socket.io
|
||||
socket.io implemantation for Vuejs 2.0 and 1.0
|
||||
|
||||
[](https://www.npmjs.com/package/vue-socket.io)
|
||||

|
||||
<a href="https://www.npmjs.com/package/vue-socket.io"><img src="https://img.shields.io/npm/dt/vue-socket.io.svg" alt="Downloads"></a>
|
||||
<img id="dependency_badge" src="https://www.versioneye.com/javascript/metinseylan:vue-socket.io/2.0.1/badge.svg" alt="Dependency Badge" rel="nofollow">
|
||||
<a href="https://www.npmjs.com/package/vue-socket.io"><img src="https://img.shields.io/npm/l/vue-socket.io.svg" alt="License"></a>
|
||||
|
||||
socket.io implemantation for Vuejs 2 and Vuex
|
||||
|
||||
## Install
|
||||
|
||||
``` bash
|
||||
npm install vue-socket.io --save
|
||||
```
|
||||
for Vue 1.0
|
||||
|
||||
``` bash
|
||||
npm install vue-socket.io@1.0.2 --save
|
||||
```
|
||||
``` bash
|
||||
npm install vue-socket.io --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
##### Configration
|
||||
Automaticly socket connect from url string
|
||||
``` js
|
||||
import Vue from 'vue';
|
||||
import VueSocketio from 'vue-socket.io';
|
||||
Vue.use(VueSocketio, 'http://socketserver.com:1923');
|
||||
```
|
||||
|
||||
Vue.use(VueSocketio, 'http://socketserver.com:1923'); // Automaticly socket connect from url string
|
||||
Bind custom socket.io-client instance
|
||||
``` js
|
||||
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));
|
||||
```
|
||||
|
||||
/*
|
||||
import socketio from 'socket.io-client';
|
||||
|
||||
var ioInstance = socketio('http://socketserver.com:1923');
|
||||
|
||||
Vue.use(VueSocketio, ioInstance); // bind custom socketio instance
|
||||
*/
|
||||
Enable Vuex integration
|
||||
``` js
|
||||
import store from './yourstore'
|
||||
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);
|
||||
```
|
||||
|
||||
##### On Vuejs instance usage
|
||||
``` js
|
||||
var vm = new Vue({
|
||||
sockets:{
|
||||
connect: function(){
|
||||
|
@ -46,10 +52,48 @@ var vm = new Vue({
|
|||
})
|
||||
```
|
||||
|
||||
##### Dynamic socket event listenlers
|
||||
Create new listenler
|
||||
``` js
|
||||
this.$options.sockets.event_name = (data) => {
|
||||
console.log(data)
|
||||
}
|
||||
```
|
||||
Remove exist listenler
|
||||
``` js
|
||||
delete this.$options.sockets.event_name;
|
||||
```
|
||||
|
||||
##### Vuex Store integration
|
||||
Example store, socket mutations always have "SOCKET_" prefix
|
||||
``` js
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
|
||||
Vue.use(Vuex);
|
||||
|
||||
export default new Vuex.Store({
|
||||
state: {
|
||||
connect: false,
|
||||
message: null
|
||||
},
|
||||
mutations:{
|
||||
SOCKET_CONNECT: (state, status ) => {
|
||||
state.connect = true;
|
||||
},
|
||||
SOCKET_USER_MESSAGE: (state, message) => {
|
||||
state.message = message;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
otherAction: ({ commit, dispatch, state }, type) => {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Example
|
||||
[Realtime Car Tracker System](http://metinseylan.com/)
|
||||
|
||||
[Simple Chat App](http://metinseylan.com/vuesocketio/)
|
||||
|
||||
## License
|
||||
[WTFPL](http://www.wtfpl.net/)
|
||||
|
|
7
dist/build.js
vendored
Normal file → Executable file
7
dist/build.js
vendored
Normal file → Executable file
File diff suppressed because one or more lines are too long
2
dist/build.js.map
vendored
Normal file → Executable file
2
dist/build.js.map
vendored
Normal file → Executable file
File diff suppressed because one or more lines are too long
18
package.json
Normal file → Executable file
18
package.json
Normal file → Executable file
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"name": "vue-socket.io",
|
||||
"version": "2.0.1",
|
||||
"description": "socket.io implemantation for vuejs",
|
||||
"version": "2.1.0",
|
||||
"description": "socket.io implemantation for vuejs and vuex",
|
||||
"main": "dist/build.js",
|
||||
"scripts": {
|
||||
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
|
||||
"build": "webpack --progress --hide-modules"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -14,9 +14,13 @@
|
|||
"vuejs",
|
||||
"socket",
|
||||
"vue",
|
||||
"socket",
|
||||
"socket.io",
|
||||
"comolokko"
|
||||
"websocket",
|
||||
"socket.io-client",
|
||||
"realtime",
|
||||
"flux",
|
||||
"vuex",
|
||||
"redux"
|
||||
],
|
||||
"author": "Metin Seylan",
|
||||
"license": "MIT",
|
||||
|
@ -31,8 +35,6 @@
|
|||
"babel-cli": "^6.11.4",
|
||||
"babel-loader": "^6.2.5",
|
||||
"babel-preset-es2015": "^6.3.13",
|
||||
"babel-preset-stage-0": "^6.3.13",
|
||||
"cross-env": "^2.0.0",
|
||||
"webpack": "^1.13.2"
|
||||
"webpack": "^2.2.0-rc.3"
|
||||
}
|
||||
}
|
||||
|
|
1
src/Emitter.js
Normal file → Executable file
1
src/Emitter.js
Normal file → Executable file
|
@ -45,4 +45,5 @@ export default new class {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
32
src/Main.js
Normal file → Executable file
32
src/Main.js
Normal file → Executable file
|
@ -3,33 +3,43 @@ import Emitter from './Emitter'
|
|||
|
||||
export default {
|
||||
|
||||
install(Vue, connection){
|
||||
install(Vue, connection, store){
|
||||
|
||||
if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
|
||||
|
||||
let observer = new Observer(connection)
|
||||
let observer = new Observer(connection, store)
|
||||
|
||||
Vue.prototype.$socket = observer.Socket;
|
||||
|
||||
Vue.mixin({
|
||||
beforeCreate(){
|
||||
let _this = this;
|
||||
let sockets = this.$options['sockets']
|
||||
|
||||
this.$options.sockets = new Proxy({}, {
|
||||
set: (target, key, value) => {
|
||||
Emitter.addListener(key, value, this)
|
||||
target[key] = value
|
||||
return true;
|
||||
},
|
||||
deleteProperty: (target, key) => {
|
||||
Emitter.removeListener(key, this.$options.sockets[key], this)
|
||||
delete target.key;
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
if(sockets){
|
||||
Object.keys(sockets).forEach(function(key) {
|
||||
Emitter.addListener(key, sockets[key], _this)
|
||||
Object.keys(sockets).forEach((key) => {
|
||||
this.$options.sockets[key] = sockets[key];
|
||||
});
|
||||
}
|
||||
|
||||
},
|
||||
beforeDestroy(){
|
||||
let _this = this;
|
||||
let sockets = this.$options['sockets']
|
||||
|
||||
if(sockets){
|
||||
Object.keys(sockets).forEach(function(key) {
|
||||
Emitter.removeListener(key, sockets[key], _this)
|
||||
Object.keys(sockets).forEach((key) => {
|
||||
delete this.$options.sockets[key]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -37,4 +47,6 @@ export default {
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
26
src/Observer.js
Normal file → Executable file
26
src/Observer.js
Normal file → Executable file
|
@ -3,7 +3,7 @@ import Socket from 'socket.io-client'
|
|||
|
||||
export default class{
|
||||
|
||||
constructor(connection) {
|
||||
constructor(connection, store) {
|
||||
|
||||
if(typeof connection == 'string'){
|
||||
this.Socket = Socket(connection);
|
||||
|
@ -11,23 +11,41 @@ export default class{
|
|||
this.Socket = connection
|
||||
}
|
||||
|
||||
if(store) this.store = store;
|
||||
|
||||
this.onEvent()
|
||||
|
||||
}
|
||||
|
||||
onEvent(){
|
||||
this.Socket.onevent = (packet) => {
|
||||
Emitter.emit(packet.data[0], packet.data[1])
|
||||
}
|
||||
Emitter.emit(packet.data[0], packet.data[1]);
|
||||
|
||||
if(this.store) this.commitStore('SOCKET_'+packet.data[0], packet.data[1])
|
||||
|
||||
};
|
||||
|
||||
let _this = this;
|
||||
|
||||
["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
|
||||
.forEach((value) => {
|
||||
_this.Socket.on(value, (data) => {
|
||||
Emitter.emit(value, data)
|
||||
Emitter.emit(value, data);
|
||||
if(_this.store) _this.commitStore('SOCKET_'+value.toUpperCase(), data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
commitStore(type, payload){
|
||||
|
||||
if(type.split('_')[0].toUpperCase() === 'SOCKET'){
|
||||
|
||||
if(this.store._mutations[type])
|
||||
this.store.commit(type, payload)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
53
webpack.config.js
Normal file → Executable file
53
webpack.config.js
Normal file → Executable file
|
@ -8,43 +8,24 @@ module.exports = {
|
|||
library: ['VueSocketio'],
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
resolveLoader: {
|
||||
root: path.join(__dirname, 'node_modules'),
|
||||
},
|
||||
module: {
|
||||
loaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
loader: 'babel',
|
||||
exclude: /node_modules/,
|
||||
query: {
|
||||
presets: ['es2015']
|
||||
}
|
||||
},
|
||||
{
|
||||
test: /\.json$/,
|
||||
loader: 'json'
|
||||
}
|
||||
]
|
||||
},
|
||||
devtool: 'eval-source-map'
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
module.exports.devtool = 'source-map'
|
||||
|
||||
module.exports.plugins = (module.exports.plugins || []).concat([
|
||||
new webpack.DefinePlugin({
|
||||
'process.env': {
|
||||
NODE_ENV: '"production"'
|
||||
}
|
||||
}),
|
||||
devtool: "source-map",
|
||||
plugins: [
|
||||
new webpack.optimize.UglifyJsPlugin({
|
||||
compress: {
|
||||
warnings: false
|
||||
}
|
||||
}),
|
||||
new webpack.optimize.OccurenceOrderPlugin()
|
||||
])
|
||||
}
|
||||
|
||||
})
|
||||
],
|
||||
module: {
|
||||
loaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules/,
|
||||
loader: 'babel-loader',
|
||||
query: {
|
||||
presets: ['es2015']
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue