From 54dd93b2c802dc3047d6a5c0cff97a6cf83335ac Mon Sep 17 00:00:00 2001 From: francisco0013 Date: Mon, 28 Jul 2025 19:21:42 -0300 Subject: [PATCH] grupo amigos --- docs/tutorial/13-ending-notes.md | 408 ++++--------------------------- 1 file changed, 54 insertions(+), 354 deletions(-) diff --git a/docs/tutorial/13-ending-notes.md b/docs/tutorial/13-ending-notes.md index c14b09db..72bddc61 100644 --- a/docs/tutorial/13-ending-notes.md +++ b/docs/tutorial/13-ending-notes.md @@ -1,359 +1,59 @@ ---- -title: Ending notes -slug: ending-notes ---- - -import Tabs from '@theme/Tabs'; -import TabItem from '@theme/TabItem'; - -# Ending notes - -## Final server code - - - - -```js title="index.js" -const express = require('express'); -const { createServer } = require('node:http'); -const { join } = require('node:path'); -const { Server } = require('socket.io'); -const sqlite3 = require('sqlite3'); -const { open } = require('sqlite'); -const { availableParallelism } = require('node:os'); -const cluster = require('node:cluster'); -const { createAdapter, setupPrimary } = require('@socket.io/cluster-adapter'); - -if (cluster.isPrimary) { - const numCPUs = availableParallelism(); - for (let i = 0; i < numCPUs; i++) { - cluster.fork({ - PORT: 3000 + i - }); - } - - return setupPrimary(); -} - -async function main() { - const db = await open({ - filename: 'chat.db', - driver: sqlite3.Database - }); - - await db.exec(` - CREATE TABLE IF NOT EXISTS messages ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - client_offset TEXT UNIQUE, - content TEXT - ); - `); - - const app = express(); - const server = createServer(app); - const io = new Server(server, { - connectionStateRecovery: {}, - adapter: createAdapter() - }); - - app.get('/', (req, res) => { - res.sendFile(join(__dirname, 'index.html')); - }); - - io.on('connection', async (socket) => { - socket.on('chat message', async (msg, clientOffset, callback) => { - let result; - try { - result = await db.run('INSERT INTO messages (content, client_offset) VALUES (?, ?)', msg, clientOffset); - } catch (e) { - if (e.errno === 19 /* SQLITE_CONSTRAINT */ ) { - callback(); - } else { - // nothing to do, just let the client retry - } - return; - } - io.emit('chat message', msg, result.lastID); - callback(); - }); - - if (!socket.recovered) { - try { - await db.each('SELECT id, content FROM messages WHERE id > ?', - [socket.handshake.auth.serverOffset || 0], - (_err, row) => { - socket.emit('chat message', row.content, row.id); - } - ) - } catch (e) { - // something went wrong - } + + + + + Chat dos Amigos + + + +

Chat dos Amigos đź’¬

+
+ +
+ + +
+ + + - - - -``` -
- - -```html title="index.html" - - - - - Socket.IO chat - - - -
    -
    - -
    - - - + socket.on('chat message', (data) => { + const msg = document.createElement('p'); + msg.textContent = `${data.nickname}: ${data.message}`; + messagesDiv.appendChild(msg); + }); + + -``` - -
    -
    - -## Homework - -Here are some ideas to improve the application: - -- Broadcast a message to connected users when someone connects or disconnects. -- Add support for nicknames. -- Don’t send the same message to the user that sent it. Instead, append the message directly as soon as they press enter. -- Add “{user} is typing” functionality. -- Show who’s online. -- Add private messaging. -- Share your improvements! - -## Getting this example - -You can find it on GitHub [here](https://github.com/socketio/chat-example). - -``` -git clone https://github.com/socketio/chat-example.git -``` - -## Next steps - -Please check out: - -- [our other examples](/get-started/) -- [our Troubleshooting guide](../categories/01-Documentation/troubleshooting.md) -- [the Emit cheatsheet](../emit-cheatsheet.md) -- [the complete Server API](../server-api.md) -- [the complete Client API](../client-api.md) -- the different sections of [our guide](../categories/01-Documentation/index.md)