Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
socket.io 'on' no se dispara con el servidor Nodejs

servidor.js

 const express_l = require('express'); const path_l = require('path'); const app_l = express_l(); const http_l = require('http'); const server_l = http_l.createServer( app_l); const socketio_l = require('socket.io'); const io_l = socketio_l( server_l); app_l.use( express_l.static( path_l.join( __dirname, 'public')) ); io_l.on( "connection", argSocket => { console.log('New websocket connection!'); }); var PORT = 3000 || process.env.PORT; server_l.listen( PORT, ()=>{ console.log(`from express server on port ${PORT}!`) });

principal.js

const socket = io_l();

charla.html

 ... ... <script src = "/socket.io/socket.io.js"> </script> <script src = "js/main.js"> </script> </body> </html>

paquete.json

 { "name": "chatcord", "version": "1.0.0", "description": "Realtime chat application built with Nodejs", "main": "server.js", "scripts": { "start": "node server", "dev": "nodemon server" }, "author": "xyz", "license": "MIT", "dependencies": { "express": "^4.18.1", "moment": "^2.29.3", "socket.io": "^4.5.1" }, "devDependencies": { "nodemon": "^2.0.16" } }

Ahora, cuando actualizo http://localhost:3000/ en el navegador, no veo la instrucción console.log de la función de on de socket.io .

¿Dónde me estoy equivocando? Por favor señale.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Su inicialización es incorrecta. Debe crear una nueva instancia del servidor socket io y luego llamarlo. Consulte la documentación para obtener más información. https://socket.io/get-started/chat#integrating-socketio

 const { Server } = require("socket.io"); const io = new Server(server); io.on('connection', (socket) => { console.log('a user connected'); });

Espero que esto ayude.

about 4 years ago · Santiago Gelvez Report

0

esto es de la documentación de socket io https://socket.io/get-started/chat

 const express = require('express'); const app = express(); const http = require('http'); const server = http.createServer(app); const { Server } = require("socket.io"); const io = new Server(server); io.on("connection", async (socket) => { console.log("someone connected to socket") //add listener here socket.on("chat",(data)=> { console.log(data) }) }); httpServer.listen(port, host, () => { console.log(`Server running on http://${host}:${port}`); });

también esta es la hoja de trucos de emisión que puede ser útil para usted https://socket.io/docs/v3/emit-cheatsheet/

about 4 years ago · Santiago Gelvez Report

0

Está consolando en el servidor nodejs, así que verifique su terminal para ver la salida de la consola. Debe investigar cómo funciona la emisión de eventos en el socket ( material de ayuda )

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!