File tree Expand file tree Collapse file tree 3 files changed +9
-28
lines changed
Expand file tree Collapse file tree 3 files changed +9
-28
lines changed Original file line number Diff line number Diff line change @@ -30,12 +30,13 @@ export default defineNuxtConfig({
3030
3131``` vue
3232<script setup>
33- // Default
34- const socket = useSocket()
33+ const { $io } = useNuxtApp()
3534
3635const connected = ref(false)
3736
3837onMounted(() => {
38+ const socket = $io('http://localhost:3000')
39+
3940 socket.on('connect', () => {
4041 connected.value = socket.connected
4142 })
@@ -44,13 +45,6 @@ onMounted(() => {
4445 connected.value = socket.connected
4546 })
4647})
47-
48- // Custom
49- const { $io } = useNuxtApp()
50-
51- onMounted(() => {
52- const socket2 = $io('http://localhost:3069')
53- })
5448</script>
5549
5650<template>
Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ import fg from 'fast-glob'
55import { Server as SocketServer , type ServerOptions } from 'socket.io'
66
77export interface ModuleOptions {
8- addPlugin : boolean
98 serverOptions : Partial < ServerOptions >
109}
1110
@@ -15,7 +14,6 @@ export default defineNuxtModule<ModuleOptions>({
1514 configKey : 'socket' ,
1615 } ,
1716 defaults : {
18- addPlugin : true ,
1917 serverOptions : { } ,
2018 } ,
2119 async setup ( options , nuxt ) {
@@ -74,20 +72,12 @@ export default defineNuxtModule<ModuleOptions>({
7472 } )
7573 }
7674
77- if ( options . addPlugin ) {
78- addPlugin ( resolve ( runtimeDir , 'plugin.client' ) )
79-
80- addImports ( [
81- {
82- name : 'useSocket' ,
83- from : resolve ( runtimeDir , 'composables' ) ,
84- } ,
85- {
86- name : 'useIO' ,
87- from : resolve ( runtimeDir , 'composables' ) ,
88- } ,
89- ] )
90- }
75+ addPlugin ( resolve ( runtimeDir , 'plugin.client' ) )
76+
77+ addImports ( {
78+ name : 'useIO' ,
79+ from : resolve ( runtimeDir , 'composables' ) ,
80+ } )
9181
9282 addServerHandler ( {
9383 middleware : true ,
Original file line number Diff line number Diff line change @@ -2,15 +2,12 @@ import { defineNuxtPlugin } from '#app'
22
33export default defineNuxtPlugin ( async ( nuxtApp ) => {
44 const { io } = await import ( 'socket.io-client' )
5- const socket = io ( )
65
7- nuxtApp . provide ( 'socket' , socket )
86 nuxtApp . provide ( 'io' , io )
97} )
108
119declare module '#app' {
1210 interface NuxtApp {
1311 $io : typeof import ( 'socket.io-client' ) [ 'io' ]
14- $socket : ReturnType < typeof import ( 'socket.io-client' ) [ 'io' ] >
1512 }
1613}
You can’t perform that action at this time.
0 commit comments