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
84
README.md
Normal file → Executable file
84
README.md
Normal file → Executable file
|
@ -1,33 +1,39 @@
|
||||||
# Vue-Socket.io
|
# 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
|
## Install
|
||||||
|
|
||||||
``` bash
|
``` bash
|
||||||
npm install vue-socket.io --save
|
npm install vue-socket.io --save
|
||||||
```
|
```
|
||||||
for Vue 1.0
|
|
||||||
|
|
||||||
``` bash
|
|
||||||
npm install vue-socket.io@1.0.2 --save
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
##### Configration
|
||||||
|
Automaticly socket connect from url string
|
||||||
``` js
|
``` js
|
||||||
import Vue from 'vue';
|
Vue.use(VueSocketio, 'http://socketserver.com:1923');
|
||||||
import VueSocketio from 'vue-socket.io';
|
```
|
||||||
|
|
||||||
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'));
|
||||||
|
```
|
||||||
|
|
||||||
/*
|
Enable Vuex integration
|
||||||
import socketio from 'socket.io-client';
|
``` js
|
||||||
|
import store from './yourstore'
|
||||||
var ioInstance = socketio('http://socketserver.com:1923');
|
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);
|
||||||
|
```
|
||||||
Vue.use(VueSocketio, ioInstance); // bind custom socketio instance
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
##### On Vuejs instance usage
|
||||||
|
``` js
|
||||||
var vm = new Vue({
|
var vm = new Vue({
|
||||||
sockets:{
|
sockets:{
|
||||||
connect: function(){
|
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
|
## Example
|
||||||
[Realtime Car Tracker System](http://metinseylan.com/)
|
[Realtime Car Tracker System](http://metinseylan.com/)
|
||||||
|
|
||||||
[Simple Chat App](http://metinseylan.com/vuesocketio/)
|
[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",
|
"name": "vue-socket.io",
|
||||||
"version": "2.0.1",
|
"version": "2.1.0",
|
||||||
"description": "socket.io implemantation for vuejs",
|
"description": "socket.io implemantation for vuejs and vuex",
|
||||||
"main": "dist/build.js",
|
"main": "dist/build.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
|
"build": "webpack --progress --hide-modules"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -14,9 +14,13 @@
|
||||||
"vuejs",
|
"vuejs",
|
||||||
"socket",
|
"socket",
|
||||||
"vue",
|
"vue",
|
||||||
"socket",
|
|
||||||
"socket.io",
|
"socket.io",
|
||||||
"comolokko"
|
"websocket",
|
||||||
|
"socket.io-client",
|
||||||
|
"realtime",
|
||||||
|
"flux",
|
||||||
|
"vuex",
|
||||||
|
"redux"
|
||||||
],
|
],
|
||||||
"author": "Metin Seylan",
|
"author": "Metin Seylan",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -31,8 +35,6 @@
|
||||||
"babel-cli": "^6.11.4",
|
"babel-cli": "^6.11.4",
|
||||||
"babel-loader": "^6.2.5",
|
"babel-loader": "^6.2.5",
|
||||||
"babel-preset-es2015": "^6.3.13",
|
"babel-preset-es2015": "^6.3.13",
|
||||||
"babel-preset-stage-0": "^6.3.13",
|
"webpack": "^2.2.0-rc.3"
|
||||||
"cross-env": "^2.0.0",
|
|
||||||
"webpack": "^1.13.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
30
src/Main.js
Normal file → Executable file
30
src/Main.js
Normal file → Executable file
|
@ -3,33 +3,43 @@ import Emitter from './Emitter'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
||||||
install(Vue, connection){
|
install(Vue, connection, store){
|
||||||
|
|
||||||
if(!connection) throw new Error("[Vue-Socket.io] cannot locate connection")
|
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.prototype.$socket = observer.Socket;
|
||||||
|
|
||||||
Vue.mixin({
|
Vue.mixin({
|
||||||
beforeCreate(){
|
beforeCreate(){
|
||||||
let _this = this;
|
|
||||||
let sockets = this.$options['sockets']
|
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){
|
if(sockets){
|
||||||
Object.keys(sockets).forEach(function(key) {
|
Object.keys(sockets).forEach((key) => {
|
||||||
Emitter.addListener(key, sockets[key], _this)
|
this.$options.sockets[key] = sockets[key];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
beforeDestroy(){
|
beforeDestroy(){
|
||||||
let _this = this;
|
|
||||||
let sockets = this.$options['sockets']
|
let sockets = this.$options['sockets']
|
||||||
|
|
||||||
if(sockets){
|
if(sockets){
|
||||||
Object.keys(sockets).forEach(function(key) {
|
Object.keys(sockets).forEach((key) => {
|
||||||
Emitter.removeListener(key, sockets[key], _this)
|
delete this.$options.sockets[key]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,3 +48,5 @@ 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{
|
export default class{
|
||||||
|
|
||||||
constructor(connection) {
|
constructor(connection, store) {
|
||||||
|
|
||||||
if(typeof connection == 'string'){
|
if(typeof connection == 'string'){
|
||||||
this.Socket = Socket(connection);
|
this.Socket = Socket(connection);
|
||||||
|
@ -11,23 +11,41 @@ export default class{
|
||||||
this.Socket = connection
|
this.Socket = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(store) this.store = store;
|
||||||
|
|
||||||
this.onEvent()
|
this.onEvent()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onEvent(){
|
onEvent(){
|
||||||
this.Socket.onevent = (packet) => {
|
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;
|
let _this = this;
|
||||||
|
|
||||||
["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
|
["connect", "error", "disconnect", "reconnect", "reconnect_attempt", "reconnecting", "reconnect_error", "reconnect_failed", "connect_error", "connect_timeout", "connecting", "ping", "pong"]
|
||||||
.forEach((value) => {
|
.forEach((value) => {
|
||||||
_this.Socket.on(value, (data) => {
|
_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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
51
webpack.config.js
Normal file → Executable file
51
webpack.config.js
Normal file → Executable file
|
@ -8,43 +8,24 @@ module.exports = {
|
||||||
library: ['VueSocketio'],
|
library: ['VueSocketio'],
|
||||||
libraryTarget: 'umd'
|
libraryTarget: 'umd'
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
devtool: "source-map",
|
||||||
root: path.join(__dirname, 'node_modules'),
|
plugins: [
|
||||||
},
|
|
||||||
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"'
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
new webpack.optimize.UglifyJsPlugin({
|
new webpack.optimize.UglifyJsPlugin({
|
||||||
compress: {
|
compress: {
|
||||||
warnings: false
|
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