简体   繁体   中英

Socket.IO request does not receive a response

I have a Node.js app using socket.io-client to send requests to a Python server using the socketio package.

In Node.js:

const sio_socket = require('socket.io-client')('http://localhost:7284');

app.post('/test', function(req, res) {
  console.log("sending test message");
  sio_socket.emit("test", "test data",
    (data) => {
      if (err) {
        console.log("error during test: " + err);
      } else {
        console.log("test answer: " + data)
      }
    });
    res.end();
});

In Python:

import socketio
import eventlet

sio = socketio.Server()
app = socketio.WSGIApp(sio)

@sio.on('connect')
def connect(sid, environ):
    print('connect ', sid)

@sio.on('test')
def respond_test(sid, data):
    print("received test message: {}".format(data))
    return "test success"

@sio.on('disconnect')
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 7284)), app)

I send a request with curl to the Node.js app:

$ curl -P 3000 -X POST localhost:3000/test

The Node app receives it:

sending test message
POST /test 200 1.263 ms - -

But the Python server prints nothing. Only when I close the Node app (with Control-C) does it print something:

127.0.0.1 - - [24/May/2019 14:42:43] "GET /socket.io/?EIO=3&transport=polling&t=Mhft8LT&b64=1&sid=75d4fb4223044bc6984d877c92132faf HTTP/1.1" 200 0 59.018009

I expect the Node app to print "test answer: test success", since that's the return value of the Python handler, and I've read that this return value is sent back to the client.

What am I doing wrong?

Sample Socket IO node client and server

Node Server version 2.2.0

const io = require('socket.io')();

io.on('connection', function (socket) {
    socket.on('ferret', function (name, word, fn) {
      fn(name + ' says ' + word);
    });
  });
io.listen(3000);

Node Client Version 2.2.0

const sio_socket = require('socket.io-client')('http://localhost:3000');

sio_socket.emit('ferret', 'tobi', 'woot',
(data) => {
    console.log(data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM