詳解vue-socket.io使用教程與踩坑記錄
請(qǐng)先允許我狠狠吐個(gè)槽:vue-socket.io相關(guān)中文博客實(shí)在太少太少,來(lái)來(lái)去去就那么幾篇,教程也比較零散,版本也比較老,就算我有暴風(fēng)式搜索還是找不到解決問題的方案,然后我怒了,開始看源碼、寫測(cè)試demo、幾乎把相關(guān)的issues都看了一遍,折騰1天后終于。。。搞定了,下面總結(jié)一下~
考慮到很多小伙伴看完文章還是一頭霧水或者無(wú)法復(fù)現(xiàn)方案,附加demo源碼https://github.com/dreamsqin/demo-vue-socket.io一份,耗時(shí)一天~滿意到話給我個(gè)start~感謝
前言
vue-socket.io其實(shí)是在socket.io-client基礎(chǔ)上做了一層封裝,將$socket掛載到vue實(shí)例上,同時(shí)你可以使用sockets對(duì)象輕松實(shí)現(xiàn)組件化的事件監(jiān)聽,讓你在vue項(xiàng)目中使用起來(lái)更方便。我目前用的vue-socket.io:3.0.7,可以在其package.json中看到它依賴于socket.io-client:2.1.1。
我遇到的問題
websocket連接地址是從后端動(dòng)態(tài)獲取,所以導(dǎo)致頁(yè)面加載時(shí)VueSocketIO實(shí)例還未創(chuàng)建,頁(yè)面中通過this.$socket.emit發(fā)起訂閱報(bào)錯(cuò),同時(shí)無(wú)法找到vue實(shí)例的sockets對(duì)象(寫在內(nèi)部的事件將無(wú)法監(jiān)聽到,就算后面已經(jīng)連接成功)
如果你的websocket連接地址是靜態(tài)的(寫死的),可以只看使用教程,如果你跟我遇到了同樣的問題,那就跳躍到解決方案
console報(bào)錯(cuò)如下:

使用教程
先拋開可能遇到的問題,按照官網(wǎng)的教程我們走一遍:
安裝
npm install vue-socket.io --save
引入(main.js)
import Vue from 'vue'
import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'
Vue.use(new VueSocketIO({
debug: true,
connection: 'http://metinseylan.com:1992',
vuex: {
store,
actionPrefix: 'SOCKET_',
mutationPrefix: 'SOCKET_'
},
options: { path: "/my-app/" } //Optional options
}))
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
debug:生產(chǎn)環(huán)境建議關(guān)閉,開發(fā)環(huán)境可以打開,這樣你就可以在控制臺(tái)看到socket連接和事件監(jiān)聽的一些信息,例如下面這樣:

connection:連接地址前綴,注意!這里只有前綴,我之前被坑過,因?yàn)槊髅骱蠖擞薪o我返回上下文,但莫名其妙的被去除了,vue-socket.io這里用到的是socket.io-client的Manager api,關(guān)鍵源碼如下(只看我寫中文備注的部分就好):
vue-socket.io(index.js)
import SocketIO from "socket.io-client";
export default class VueSocketIO {
/**
* lets take all resource
* @param io
* @param vuex
* @param debug
* @param options
*/
constructor({connection, vuex, debug, options}){
Logger.debug = debug;
this.io = this.connect(connection, options); // 獲取到你設(shè)定的參數(shù)后就調(diào)用了connect方法
this.useConnectionNamespace = (options && options.useConnectionNamespace);
this.namespaceName = (options && options.namespaceName);
this.emitter = new Emitter(vuex);
this.listener = new Listener(this.io, this.emitter);
}
/**
* registering SocketIO instance
* @param connection
* @param options
*/
connect(connection, options) {
if (connection && typeof connection === "object") {
Logger.info(`Received socket.io-client instance`);
return connection;
} else if (typeof connection === "string") {
const io = SocketIO(connection, options);// 其實(shí)用的是socket.io-client的Manager API
Logger.info(`Received connection string`);
return (this.io = io);
} else {
throw new Error("Unsupported connection type");
}
}
socket.io-client(index.js)
var url = require('./url');
function lookup (uri, opts) {
if (typeof uri === 'object') {
opts = uri;
uri = undefined;
}
opts = opts || {};
var parsed = url(uri); // 通過url.js對(duì)connection前綴進(jìn)行截取
var source = parsed.source;
var id = parsed.id;
var path = parsed.path;
var sameNamespace = cache[id] && path in cache[id].nsps;
var newConnection = opts.forceNew || opts['force new connection'] ||
false === opts.multiplex || sameNamespace;
var io;
if (newConnection) {
debug('ignoring socket cache for %s', source);
io = Manager(source, opts);
} else {
if (!cache[id]) {
debug('new io instance for %s', source);
cache[id] = Manager(source, opts);
}
io = cache[id];
}
if (parsed.query && !opts.query) {
opts.query = parsed.query;
}
return io.socket(parsed.path, opts);// 實(shí)際調(diào)用的是解析后的前綴地址
}
options.path: 這里就可以填websocket連接地址的后綴,如果不填會(huì)被默認(rèn)添加/socket.io,關(guān)鍵源碼如下(只看我寫中文備注的部分就好):
其他的options配置可以參見https://socket.io/docs/client-api/
socket.io-client(manager.js)
function Manager (uri, opts) {
if (!(this instanceof Manager)) return new Manager(uri, opts);
if (uri && ('object' === typeof uri)) {
opts = uri;
uri = undefined;
}
opts = opts || {};
opts.path = opts.path || '/socket.io'; // 看到?jīng)]有,如果你不傳遞options.path參數(shù)的話會(huì)被默認(rèn)安一個(gè)尾巴"/socket.io"
this.nsps = {};
this.subs = [];
this.opts = opts;
this.reconnection(opts.reconnection !== false);
this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
this.reconnectionDelay(opts.reconnectionDelay || 1000);
this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
this.randomizationFactor(opts.randomizationFactor || 0.5);
this.backoff = new Backoff({
min: this.reconnectionDelay(),
max: this.reconnectionDelayMax(),
jitter: this.randomizationFactor()
});
this.timeout(null == opts.timeout ? 20000 : opts.timeout);
this.readyState = 'closed';
this.uri = uri;
this.connecting = [];
this.lastPing = null;
this.encoding = false;
this.packetBuffer = [];
var _parser = opts.parser || parser;
this.encoder = new _parser.Encoder();
this.decoder = new _parser.Decoder();
this.autoConnect = opts.autoConnect !== false;
if (this.autoConnect) this.open();
}
vuex: 配置后可以在store.js的mutations或者actions監(jiān)聽到Vue-Socket.io事件(例如:connect、disconnect、reconnect等),這部分目前用得比較少,也挺簡(jiǎn)單,如果有疑問可以給我留言我再單獨(dú)提供教程。
使用(Page.vue)
注意:熟悉socket.io-client的應(yīng)該知道,默認(rèn)情況下,websocket在創(chuàng)建實(shí)例的時(shí)候就會(huì)自動(dòng)發(fā)起連接了,所以切記不要在組件中重復(fù)發(fā)起連接。如果你想自己控制發(fā)起連接的時(shí)機(jī)可以將options.autoConnect設(shè)置為false。
export default {
name: 'Page',
sockets: {// 通過vue實(shí)例對(duì)象sockets實(shí)現(xiàn)組件中的事件監(jiān)聽
connect: function () {// socket的connect事件
console.log('socket connected from Page')
},
STREAM_STATUS(data) {// 后端按主題名推送的消息數(shù)據(jù)
console.log('Page:' + data)
}
},
mounted() {
console.log('page mounted')
this.$socket.emit('STREAM_STATUS', { subscribe: true })// 在頁(yè)面加載時(shí)發(fā)起訂閱,“STREAM_STATUS”是你跟后端約定好的主題名
}
}
事件除了在sockets對(duì)象中默認(rèn)監(jiān)聽,你還可以在外部單獨(dú)注冊(cè)事件監(jiān)聽或取消注冊(cè):
this.sockets.subscribe('EVENT_NAME', (data) => {
this.msg = data.message;
});
this.sockets.unsubscribe('EVENT_NAME');
但這種方式從源碼上看是不支持參數(shù)傳遞的,只支持傳遞事件名及回調(diào)函數(shù)(部分源碼如下):
vue-Socket.io(mixin.js)
beforeCreate(){
if(!this.sockets) this.sockets = {};
if (typeof this.$vueSocketIo === 'object') {
for (const namespace of Object.keys(this.$vueSocketIo)) {
this.sockets[namespace] = {
subscribe: (event, callback) => {
this.$vueSocketIo[namespace].emitter.addListener(event, callback, this);
},
unsubscribe: (event) => {
this.$vueSocketIo[namespace].emitter.removeListener(event, this);
}
}
}
} else {
this.$vueSocketIo.emitter.addListener(event, callback, this);
this.$vueSocketIo.emitter.removeListener(event, this);
}
}
解決方案
針對(duì)我上面描述的問題,最大原因就在于獲取socket連接地址是異步請(qǐng)求,如文章開頭的截圖,page mounted打印時(shí),this.$socket還是undefined。所以我們要做的就是怎么樣讓頁(yè)面加載在VueSocketIO實(shí)例創(chuàng)建之后。
我提供兩種解決方案,具體怎么選擇看你們的需求~
保證拿到socket連接地址后再將vue實(shí)例掛載到app
缺點(diǎn):如果你獲取socket地址的請(qǐng)求失敗了,整個(gè)項(xiàng)目的頁(yè)面都加載不出來(lái)(一般服務(wù)器出現(xiàn)問題才會(huì)有這種情況產(chǎn)生)
優(yōu)點(diǎn):實(shí)現(xiàn)簡(jiǎn)單,一小段代碼挪個(gè)位置就好
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ParentApi from '@/api/Parent'
import VueSocketIO from 'vue-socket.io'
/* 使用vue-socket.io */
ParentApi.getSocketUrl().then((res) => {
Vue.use(new VueSocketIO({
debug: false,
connection: res.data.path,
options: { path: '/my-project/socket.io' }
}))
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
})
控制臺(tái)打印如下圖:

結(jié)合connect事件+store+路由守衛(wèi)實(shí)現(xiàn)攔截
原理:異步請(qǐng)求回調(diào)中創(chuàng)建VueSocketIO實(shí)例并監(jiān)聽connect事件,監(jiān)聽回調(diào)中修改isSuccessConnect參數(shù)的值,在Page頁(yè)面路由中增加beforeEnter守衛(wèi),利用setInterval周期性判斷isSuccessConnect的值,滿足條件則取消定時(shí)執(zhí)行并路由跳轉(zhuǎn)。
缺點(diǎn):實(shí)現(xiàn)起來(lái)稍微復(fù)雜一點(diǎn)
優(yōu)點(diǎn):不會(huì)影響其他頁(yè)面的加載
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ParentApi from '@/api/Parent'
import VueSocketIO from 'vue-socket.io'
ParentApi.getSocketUrl().then((res) => {
let vueSocketIo = new VueSocketIO({
debug: false,
connection: res.data.path,
options: { path: '/my-project/socket.io' }
})
// 監(jiān)聽connect事件,設(shè)置isSuccessConnect為true
vueSocketIo.io.on('connect', () => {
console.log('socket connect from main.js')
store.commit('newIsSuccessConnect', true)
})
Vue.use(vueSocketIo)
})
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
// socket連接狀態(tài)
isSuccessConnect: false
},
mutations: {
newIsSuccessConnect(state, value) {
state.isSuccessConnect = value
}
},
getters: {
getIsSuccessConnect: state => {
return state.isSuccessConnect
}
},
actions: {
}
})
router.js
import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/page',
name: 'Page',
component: () => import(/* webpackChunkName: "Page" */ './pages/Page.vue'),
beforeEnter: (to, from, next) => {
let intervalId = setInterval(() => {
// 直到store中isSuccessConnect為true時(shí)才能進(jìn)入/page
if (store.getters.getIsSuccessConnect) {
clearInterval(intervalId)
next()
}
}, 500)
}
}
]
})
控制臺(tái)打印如下圖:

參考資料:
1、vue-socket.io:https://github.com/MetinSeylan/Vue-Socket.io
2、socket.io-client:https://github.com/socketio/socket.io-client
3、vue-router守衛(wèi):https://router.vuejs.org/zh/guide/advanced/navigation-guards.html
到此這篇關(guān)于詳解vue-socket.io使用教程與踩坑記錄 的文章就介紹到這了,更多相關(guān)vue-socket.io使用教程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.set與this.$set的用法與使用場(chǎng)景介紹
Vue.set()和this.$set()這兩個(gè)api的實(shí)現(xiàn)原理基本一模一樣,都是使用了set函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue.set與this.$set的用法與使用場(chǎng)景,需要的朋友可以參考下2022-03-03
Vue-admin-template?報(bào)Uncaught?(in?promise)?error問題及解決
這篇文章主要介紹了Vue-admin-template?報(bào)Uncaught?(in?promise)?error問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
使用Vue實(shí)現(xiàn)移動(dòng)端左滑刪除效果附源碼
這篇文章主要介紹了使用Vue實(shí)現(xiàn)移動(dòng)端左滑刪除效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
vue + typescript + video.js實(shí)現(xiàn) 流媒體播放 視頻監(jiān)控功能
視頻才用流媒體,有后臺(tái)實(shí)時(shí)返回?cái)?shù)據(jù), 要支持flash播放, 所以需安裝對(duì)應(yīng)的flash插件。這篇文章主要介紹了vue + typescript + video.js 流媒體播放 視頻監(jiān)控,需要的朋友可以參考下2019-07-07
Vue異步更新DOM及$nextTick執(zhí)行機(jī)制解讀
這篇文章主要介紹了Vue異步更新DOM及$nextTick執(zhí)行機(jī)制解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue中控制mock在開發(fā)環(huán)境使用,在生產(chǎn)環(huán)境禁用方式
這篇文章主要介紹了vue中控制mock在開發(fā)環(huán)境使用,在生產(chǎn)環(huán)境禁用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

