from flask import render_template, request, session, copy_current_request_context, current_app
from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect
from threading import Lock
from app import socketio

# async_mode = None

# socketio = SocketIO(current_app, async_mode=None)
thread = None
thread_lock = Lock()


# def background_thread():
#     """Example of how to send server generated events to clients."""
#     count = 0
#     while True:
#         socketio.sleep(10)
#         count += 1
#         socketio.emit('my_response',
#                       {'data': 'Server generated event', 'count': count},
#                       namespace='/test')


@socketio.on('my_event', namespace='/test')
def test_message(message):
    print("test_message {}".format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']})


@socketio.on('my_broadcast_event', namespace='/test')
def test_broadcast_message(message):
    print("my_broadcast_event message {}".format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']},
         broadcast=True)


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': 'In rooms: ' + ', '.join(rooms()),
          'count': session['receive_count']})


@socketio.on('leave', namespace='/test')
def leave(message):
    print('leave')
    leave_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': 'In rooms: ' + ', '.join(rooms()),
          'count': session['receive_count']})


@socketio.on('close_room', namespace='/test')
def close(message):
    print('close_room')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.',
                         'count': session['receive_count']},
         room=message['room'])
    close_room(message['room'])


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']},
         room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()

    session['receive_count'] = session.get('receive_count', 0) + 1
    # for this emit we use a callback function
    # when the callback function is invoked we know that the message has been
    # received and it is safe to disconnect
    emit('my_response',
         {'data': 'Disconnected!', 'count': session['receive_count']},
         callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print("my_ping")
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    # global thread
    # with thread_lock:
    #     if thread is None:
    #         thread = socketio.start_background_task(background_thread)
    emit('my_response', {'data': 'Connected', 'count': 0})


@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected', request.sid)


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


@socketio.on('message')
def handle_message(message):
    print('received message: ' + message)


if __name__ == '__main__':
    print("if __name__ == '__main__':")
    socketio.run(current_app, debug=True)

b576b3454f2a770496348a5ef3724baa6c4e3df3
<mask token>


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count':
        session['receive_count']})


<mask token>


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Disconnected!', 'count': session[
        'receive_count']}, callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print('my_ping')
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    emit('my_response', {'data': 'Connected', 'count': 0})


<mask token>


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


<mask token>

<mask token>


@socketio.on('my_broadcast_event', namespace='/test')
def test_broadcast_message(message):
    print('my_broadcast_event message {}'.format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, broadcast=True)


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count':
        session['receive_count']})


<mask token>


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Disconnected!', 'count': session[
        'receive_count']}, callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print('my_ping')
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    emit('my_response', {'data': 'Connected', 'count': 0})


<mask token>


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


@socketio.on('message')
def handle_message(message):
    print('received message: ' + message)


<mask token>

<mask token>


@socketio.on('my_event', namespace='/test')
def test_message(message):
    print('test_message {}'.format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']})


@socketio.on('my_broadcast_event', namespace='/test')
def test_broadcast_message(message):
    print('my_broadcast_event message {}'.format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, broadcast=True)


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count':
        session['receive_count']})


<mask token>


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Disconnected!', 'count': session[
        'receive_count']}, callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print('my_ping')
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    emit('my_response', {'data': 'Connected', 'count': 0})


@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected', request.sid)


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


@socketio.on('message')
def handle_message(message):
    print('received message: ' + message)


<mask token>

<mask token>
thread = None
thread_lock = Lock()


@socketio.on('my_event', namespace='/test')
def test_message(message):
    print('test_message {}'.format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']})


@socketio.on('my_broadcast_event', namespace='/test')
def test_broadcast_message(message):
    print('my_broadcast_event message {}'.format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, broadcast=True)


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count':
        session['receive_count']})


@socketio.on('leave', namespace='/test')
def leave(message):
    print('leave')
    leave_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count':
        session['receive_count']})


@socketio.on('close_room', namespace='/test')
def close(message):
    print('close_room')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.',
        'count': session['receive_count']}, room=message['room'])
    close_room(message['room'])


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': message['data'], 'count': session[
        'receive_count']}, room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Disconnected!', 'count': session[
        'receive_count']}, callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print('my_ping')
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    emit('my_response', {'data': 'Connected', 'count': 0})


@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected', request.sid)


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


@socketio.on('message')
def handle_message(message):
    print('received message: ' + message)


if __name__ == '__main__':
    print("if __name__ == '__main__':")
    socketio.run(current_app, debug=True)

from flask import render_template, request, session, copy_current_request_context, current_app
from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect
from threading import Lock
from app import socketio

# async_mode = None

# socketio = SocketIO(current_app, async_mode=None)
thread = None
thread_lock = Lock()


# def background_thread():
#     """Example of how to send server generated events to clients."""
#     count = 0
#     while True:
#         socketio.sleep(10)
#         count += 1
#         socketio.emit('my_response',
#                       {'data': 'Server generated event', 'count': count},
#                       namespace='/test')


@socketio.on('my_event', namespace='/test')
def test_message(message):
    print("test_message {}".format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']})


@socketio.on('my_broadcast_event', namespace='/test')
def test_broadcast_message(message):
    print("my_broadcast_event message {}".format(message))
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']},
         broadcast=True)


@socketio.on('join', namespace='/test')
def join(message):
    print('join')
    join_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': 'In rooms: ' + ', '.join(rooms()),
          'count': session['receive_count']})


@socketio.on('leave', namespace='/test')
def leave(message):
    print('leave')
    leave_room(message['room'])
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': 'In rooms: ' + ', '.join(rooms()),
          'count': session['receive_count']})


@socketio.on('close_room', namespace='/test')
def close(message):
    print('close_room')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.',
                         'count': session['receive_count']},
         room=message['room'])
    close_room(message['room'])


@socketio.on('my_room_event', namespace='/test')
def send_room_message(message):
    print('my_room_event')
    session['receive_count'] = session.get('receive_count', 0) + 1
    emit('my_response',
         {'data': message['data'], 'count': session['receive_count']},
         room=message['room'])


@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    print('disconnect_request')

    @copy_current_request_context
    def can_disconnect():
        disconnect()

    session['receive_count'] = session.get('receive_count', 0) + 1
    # for this emit we use a callback function
    # when the callback function is invoked we know that the message has been
    # received and it is safe to disconnect
    emit('my_response',
         {'data': 'Disconnected!', 'count': session['receive_count']},
         callback=can_disconnect)


@socketio.on('my_ping', namespace='/test')
def ping_pong():
    print("my_ping")
    emit('my_pong')


@socketio.on('connect', namespace='/test')
def test_connect():
    print('connect')
    # global thread
    # with thread_lock:
    #     if thread is None:
    #         thread = socketio.start_background_task(background_thread)
    emit('my_response', {'data': 'Connected', 'count': 0})


@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected', request.sid)


@socketio.on('json')
def handle_json(json):
    print('received json: ' + str(json))


@socketio.on('message')
def handle_message(message):
    print('received message: ' + message)


if __name__ == '__main__':
    print("if __name__ == '__main__':")
    socketio.run(current_app, debug=True)
