I have the following system: [Clietn] --- [Web Server (Flask)] --- [Data source].
The data source connects to the server and sends new data endlessly. The server, having received the new data, sends it to the client. The exchange is organized using the Socket.IO library.
[Clietn (JS) socket.io)] --- [python-flask-socketio Server python-flask-socketio] --- [python-socketio Data source]
The code
Clietn
window.onload = function() {
// var socket = io();
var socket = io.connect('http://' + document.domain + ':' + location.port + '/');
socket.on('connect', function() {
// Socket.IO подсоединен
let p = document.getElementById('connect_status');
p.innerHTML = 'Connect';
});
socket.on('disconnect', function(disconnectReason, errorMessage) {
// Socket.IO отсоединен
let p = document.getElementById('connect_status');
p.innerHTML = 'Disconnect';
});
socket.on('my_response', function(msg) {
console.log(msg);
});
};
Server (Flask)
...
socketio = SocketIO()
...
socketio.init_app(app, async_mode='gevent')
...
###############################################################################
# Client
###############################################################################
@sio.on('connect', namespace='/')
def on_connect():
print request.namespace
# global thread, client_count
# client_count += 1
# with thread_lock:
# if thread is None:
# thread = sio.start_background_task(background_thread)
print('Client {} connected'.format(request.sid))
@sio.on('disconnect', namespace='/')
def on_disconnect():
# global thread, client_count
# client_count -= 1
# if client_count == 0:
# thread.join()
# thread = None
print('Client {} disconnected'.format(request.sid))
@sio.on('tmp', namespace='/')
def on_tmp(data):
print data
sio.emit('my_response', {'data': 'Server generated event'}, namespace='/')
###############################################################################
# Connector
###############################################################################
@sio.on('tag_changed', namespace='/connector')
def on_tag_changed(data):
print("{2}: {0} = {1}".format(data['tagName'], data['tagVal'], data['tv_sec']))
sio.emit('my_response', {'data': data}) # inform customer
@sio.on('connect', namespace='/connector')
def on_connect_connector():
print('Client (connector) {} connected'.format(request.sid))
@sio.on('disconnect', namespace='/connector')
def on_connect_connector():
print('Client (connector) {} disconnect'.format(request.sid))
Dara Source
import ctypes
import threading
import socketio
async_mode = 'gevent'
# sio = socketio.Server(async_mode=async_mode)
# app = socketio.WSGIApp(sio)
sio = socketio.Client()
thread = None
thread_lock = threading.Lock()
work = False
ktmIPC = ctypes.CDLL("./pe2.so")
ktmIPC.init_conn()
def background_thread():
class timeval(ctypes.Structure):
_fields_ = [("tv_sec", ctypes.c_long), ("tv_usec", ctypes.c_long)]
def on_tag_changed(tag_name, tag_val, tv):
sio.emit('tag_changed',
{'tagName': tag_name, 'tagVal': tag_val, 'tv_sec': tv.tv_sec},
namespace='/connector')
print("{2}: {0} = {1}".format(tag_name, tag_val, tv.tv_sec))
c_cb = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_double, timeval)(on_tag_changed)
ktmIPC.set_tag_change_callback(c_cb)
while work:
ktmIPC.conn_proc()
sio.sleep(0.1)
@sio.on('connect', namespace='/connector')
def connect():
print 'Client {} connect.'.format(sio.sid)
global thread, work
work = True
with thread_lock:
if thread is None:
thread = sio.start_background_task(background_thread)
@sio.on('disconnect', namespace='/connector')
def disconnect():
global work
work = False
print 'Client {} disconnect'.format("sid")
if __name__ == '__main__':
print '\n---------- START CLIENT ----------\n'
sio.connect('http://192.168.3.58:5000', namespaces=['/connector'])
Why, when connecting the connector to the server, the "@ sio.on ('connect', namespace = '/')" handler fires? It also activates the handler "@ sio.on ('tag_changed', namespace = '/ connector')".
sio.connect ('http://192.168.3.58:5000/connector', namespaces = ['/ connector'])
did not help either.