Unidentified reason for duplication of messages received via websocket

Viewed 53

I have a problem with an exchange of information between two machines by socket.io library.

When I'm using a both servers in node.js and socket.io library I can achieve one response for every event.

Node.js approach

Main Server

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

const server = http.createServer()

server.listen(3000);
io = io.listen(server); 

console.log('=== Main Server ===')
// Add a connect listener
io.sockets.on('connection', function(socket)
{
  console.log('Camera Server connected to Main Machine');

  socket.on('image', function (data) {

    let image = data[0]
    console.log('Camera Server sends data with ID:', data[1], typeof(image));
    let ID = data[1]

    socket.emit('response', "Main Server says: Camera Server thanks for data with ID:" + ID++);
    });

  socket.on('disconnect', function() {
  console.log('Camera Server disconnected.');
  });
});

Client Server

var io = require('socket.io-client');

const socket = io.connect('http://192.168.1.16:3000/', {
    reconnection: true
});

const wCap = new cv.VideoCapture(0);
wCap.set(cv.CAP_PROP_FRAME_WIDTH, 500);
wCap.set(cv.CAP_PROP_FRAME_HEIGHT, 400);

console.log('=== Camera Server ===')
let counter = 0


socket.on('connect', function () {

    console.log('Connection with Main Machine performed');

    setInterval(() => {
        socket.emit('image', counter++)
    }, 1500);

    socket.on('response', function (data) {
        console.log(data);
    });

    socket.on('disconnect', function() {
        console.log('Connection with Main Server was ended.');
    });
});

Terminals Output for Node approach

https://i.imgur.com/neuhgfp.png

BUT,

I have to replace my Main Server with analogical Flask server. To do that I received that code:

Flask Main Server - eventlet

from flask import Flask
from flask_socketio import SocketIO, emit, send
from random import randint
from flask_cors import cross_origin

app = Flask(_name_)
socketio = SocketIO(app, cors_allowed_origins="*")

@app.route('/')
def index():
    return render_template('index.html')


def message_received(methods=['GET', 'POST']):
    print('Message received!')

@socketio.on('image')
def test_message(data):
    print('Message received.', data)
    emit('response', data + 1, callback=message_received)

    print(response)


@socketio.on('connect')
def test_connect():
    print('Connected')
    emit('response', {'data': 'Connected'})

@socketio.on('disconnect')
def test_disconnect():
    print('Client disconnected')

if _name_ == "_main_":
    print('Works')
    socketio.run(app, host='192.168.1.16', port=3000)

The problem with this solution is that the connection between this two servers take a lot of time - much more than in homogeneous Node.js solution, and also what is the most important at Node-Client side I'm receiving a duplicated responses. There are also a lot of problems with making and maintaining a connections - we have to restart servers a lot of times to performed just one test.

Terminals Output for 1. Flask / 2. Node approach

  1. https://i.imgur.com/Rg190R7.png
  2. https://i.imgur.com/GvWIpZy.png

What is the cause of this problem and how to solve this?

0 Answers
Related