Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
Convert socket.io Javascript code to python-socketio

I have a javascript using socket.io.
I would like to convert this javascript file to python:

io.on('connection',(socket)=>{
    
    let nsData = namespaces.map((ns)=>{
        return {
            img: ns.image,
            endpoint: ns.endpoint
        }
    })
    // console.log(nsData)
    socket.emit('nsList',nsData);
})

This is what I have in python:

@sio.event
def connect(socket_id, environ):
    # ???
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

On the documentation, it is stated that you just need to either use a return statement or use the .emit() function. https://python-socketio.readthedocs.io/en/latest/server.html#emitting-events As the docs say, you simply need to

sio.emit('nsList', nsData, room=socket_id)

That will emit the nsList event with the content nsData. However, I do not know what nsData is. It seems to simply be a map statement, which can be done with the map() function. As GFG states, you simply need to map out the namespaces. https://www.geeksforgeeks.org/python-map-function/ Assuming namespaces is a list, you can do something similar to this:

def mapNsData(ns):
    return { 'img': ns.image, 'endpoint': ns.endpoint }

Then, use the map function:

nsData = list(map(mapNsData, namespaces))

Tada! Now, you have a list to send to the client!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda