diff --git a/modules/socket.js b/modules/socket.js new file mode 100644 index 0000000..7bd5b05 --- /dev/null +++ b/modules/socket.js @@ -0,0 +1,42 @@ +import { Server } from 'socket.io' + +export default (_, nuxt) => { + nuxt.hook('listen', server => { + const io = new Server(server) + + nuxt.hook('close', () => io.close()) + + io.on('connection', (socket) => { + console.log('Connection', socket.id) + }) + + io.on('connect', (socket) => { + socket.emit('message', `welcome ${socket.id}`) + socket.broadcast.emit('message', `${socket.id} joined`) + + socket.on('joinRoom', (room) => { + socket.join(room) + socket.emit('message', `joinRoom ${room}`) + socket.broadcast.to(room).emit('message', `${socket.id} joined ${room}`) + }) + + socket.on('newComment', + function comment(message, room) { + console.log('new comment received: %s', message) + socket.broadcast.to(room).emit('newComment', { message }) + }) + + socket.on('message', + function message(data) { + console.log('message received: %s', data) + socket.broadcast.emit('message', { data }) + }) + + socket.on('disconnecting', + () => { + console.log('disconnected', socket.id) + socket.broadcast.emit('message', `${socket.id} left`) + }) + }) + }) +} diff --git a/nuxt.config.ts b/nuxt.config.ts index d680623..14c301c 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -20,5 +20,8 @@ export default defineNuxtConfig({ }), ], }, - + modules: ["./modules/socket"], + plugins: [ + { src:'@/plugins/socket.client.js', mode: 'client' } + ], }) diff --git a/package.json b/package.json index 9ab9204..cb8e144 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,8 @@ "better-sqlite3": "^7.6.2", "multer": "^1.4.5-lts.1", "serve-static": "^1.15.0", + "socket.io": "^4.5.1", + "socket.io-client": "^4.5.1", "uuid": "^8.3.2" } } diff --git a/pages/c/[cosetta].vue b/pages/c/[cosetta].vue index 3431f9b..0537d39 100644 --- a/pages/c/[cosetta].vue +++ b/pages/c/[cosetta].vue @@ -1,5 +1,6 @@