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

297
Views
Socket.io proporciona la misma dirección IP sin importar desde dónde se realice la conexión (alojamiento con repl.it)

Esto es para un proyecto más grande, pero logré condensarlo en un programa pequeño.

Estoy tratando de obtener la dirección IP del cliente desde una conexión websocket. Descubrí que puedo usar socket.handshake.address, pero cuando lo ejecuto en repl.it, siempre dice que la dirección IP es 172.18.0.1.

Índice.js

 const express = require('express'); const http = require('http').createServer(); const app = require('express')(); const server = require('http').createServer(app); const io = require('socket.io')(server); io.on('connection', () => { /* … */ }); server.listen(3000); app.get('/', (request, response) => { response.sendFile('/home/runner/basicSocketio/index.html'); }); io.on('connection', (socket) => { io.emit("message", "hello client") socket.on('message', (message) => { console.log(socket.handshake.address)//always prints ::ffff:172.18.0.1 }) })

índice.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-9"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src = "https://cdn.socket.io/socket.io-3.0.0.js"></script> </head> <body> test </body> </html> <script> const socket = io(); socket.emit('message', "hi server"); socket.on('message', text => { console.log("recieved: " + text); }); </script>

No estoy seguro de por qué sucede esto y no puedo encontrar ninguna solución, cuando ejecuté esto localmente, parecía funcionar bien. ¿Que puedo hacer para arreglarlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

172.18.0.1 es la dirección interna del servidor HTML de repl.it, ya que le sirven el index.html y es allí donde realiza la llamada a const socket = io() .

Para ver la ip real del cliente, use socket.handshake.headers['x-forwarded-for'] como en:

 io.on('connection', (socket) => { io.emit('message', 'hello client') socket.on('message', (text) => { console.log(text) // console.log(socket.handshake.address) // Server serving index.html file IP address }) console.log('a client connected') console.log('client IP addr: ' + socket.handshake.headers['x-forwarded-for']) // (REAL) client IP })

about 4 years ago · Juan Pablo Isaza 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!