mod*_*7ex 3 javascript socket.io vue.js
我如何访问vue.js组件中设置函数内的套接字实例
我用vue-3-socket.io
在我的main.js
import VueSocketIO from 'vue-3-socket.io'
import SocketIO from 'socket.io-client'
Vue.use(new VueSocketIO({
debug: true,
connection: SocketIO('http://metinseylan.com:1992', {}),
vuex: {
store,
actionPrefix: "SOCKET_",
mutationPrefix: "SOCKET_"
}
})
);
Run Code Online (Sandbox Code Playgroud)
我将直接使用socket.io,而不使用Vue插件:
export const useSocketIO = () => {
const socket = io('http://metinseylan.com:1992')
return {
socket,
}
}
Run Code Online (Sandbox Code Playgroud)
Component
<script setup>
import { defineComponent } from 'vue'
const { socket } = useSocketIO()
socket.on('welcome', () => { console.log('welcome') })
</script>
Run Code Online (Sandbox Code Playgroud)