0

我有以下系统:[Clietn] --- [Web Server (Flask)] --- [Data source]。

数据源连接到服务器并无休止地发送新数据。服务器收到新数据后,将其发送给客户端。交换是使用 Socket.IO 库组织的。

[ Clietn (JS) socket.io)] --- [python-flask-socketio服务器python-flask-socketio] --- [python-socketio数据源]

编码

客户

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);
    });
};

服务器(烧瓶)

...
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))

达拉源

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'])

为什么在将连接器连接到服务器时,会触发“@sio.on ('connect', namespace = '/')”处理程序?它还激活处理程序“@sio.on ('tag_changed', namespace = '/connector')”。

sio.connect ('http://192.168.3.58:5000/connector', namespaces = ['/ connector'])也没有帮助。

4

0 回答 0