You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constKoa=require('koa');constapp=newKoa();constserver=require('http').Server(app.callback());constio=require('socket.io')(server);constport=8081;server.listen(process.env.PORT||port,()=>{console.log(`app run at : http://127.0.0.1:${port}`);})io.on('connection',socket=>{console.log('初始化成功!下面可以用socket绑定事件和触发事件了');socket.on('send',data=>{console.log('客户端发送的内容:',data);socket.emit('getMsg','我是返回的消息... ...');})setTimeout(()=>{socket.emit('getMsg','我是初始化3s后的返回消息... ...')},3000)})
Vue + Koa2 + Socket.io
依赖库版本信息
vue:2.6.11
koa:2.7.0
socket.io:3.03
socket.io-client:3.03
Koa2集成
如果是在不同源下进行
socket
连接,需要,在创建io
实例时将配置项cors
打开:Vue集成
如果想在组件内部使用
socket
实例 ,可以使用vue-socket.io
,我是将socket
实例绑定在vuex
上,所以只需安装官网要求的socket.io-client
即可。The text was updated successfully, but these errors were encountered: