added websocket test flies
This commit is contained in:
@@ -1,20 +1,52 @@
|
||||
# Copyright (c) 2019. Tobias Kurze
|
||||
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
from flask import Flask, request
|
||||
from flask_socketio import SocketIO, emit
|
||||
|
||||
from backend import app
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
socketio = SocketIO(app)
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'secret!'
|
||||
|
||||
async_mode = 'threading'
|
||||
async_mode = None
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", async_mode=async_mode)
|
||||
thread = None
|
||||
thread_lock = threading.Lock()
|
||||
|
||||
clients = []
|
||||
|
||||
|
||||
def background_thread():
|
||||
"""Example of how to send server generated events to clients."""
|
||||
count = 0
|
||||
while True:
|
||||
socketio.sleep(3)
|
||||
print("server_event ")
|
||||
count += 1
|
||||
socketio.emit('server_event',
|
||||
{'data': 'Server generated event', 'count': count},
|
||||
)
|
||||
|
||||
|
||||
def ack():
|
||||
print('message was received!')
|
||||
|
||||
|
||||
@socketio.on('my_event')
|
||||
def test_message(message: dict):
|
||||
print("received msg!")
|
||||
print(message)
|
||||
# emit('my_event', {'data': message['data']})
|
||||
if isinstance(message, str):
|
||||
emit('my_response', 'tolle Antwort! (got only a string!)', callback=ack)
|
||||
else:
|
||||
emit('my_response', {'data': message['data']}, callback=ack)
|
||||
|
||||
|
||||
@socketio.on('my broadcast event', namespace='/')
|
||||
@@ -25,7 +57,11 @@ def test_message(message):
|
||||
@socketio.on('connect')
|
||||
def test_connect():
|
||||
print("connected")
|
||||
emit('connect', "antwort")
|
||||
global thread
|
||||
with thread_lock:
|
||||
if thread is None:
|
||||
thread = socketio.start_background_task(background_thread)
|
||||
emit('connect', "You are connected!")
|
||||
|
||||
|
||||
@socketio.on('disconnect', namespace='/')
|
||||
@@ -33,10 +69,55 @@ def test_disconnect():
|
||||
print('Client disconnected')
|
||||
|
||||
|
||||
@socketio.on_error()
|
||||
def handle_error(error):
|
||||
print(error)
|
||||
|
||||
|
||||
def start_in_thread():
|
||||
socket_thread = threading.Thread(target=lambda appl, host, port: socketio.run(app=appl, host=host, port=port),
|
||||
args=(app, "localhost", 5000))
|
||||
socket_thread.start()
|
||||
return socket_thread
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def root():
|
||||
return """<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
var socket = io();
|
||||
socket.on('connect', function() {
|
||||
console.log("connected!");
|
||||
socket.emit('my_event', {data: 'I am connected!'});
|
||||
});
|
||||
socket.on('my_response', function(resp) {
|
||||
console.log('got response:');
|
||||
console.log(resp);
|
||||
});
|
||||
</script>"""
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("running main")
|
||||
socketio.init_app(app, cors_allowed_origins="*")
|
||||
socketio.run(app, host="localhost", port=5000)
|
||||
# socketio.init_app(app, cors_allowed_origins="*")
|
||||
|
||||
#while len(clients) <= 0:
|
||||
# time.sleep(1)
|
||||
|
||||
#while True:
|
||||
# time.sleep(2)
|
||||
# print("sending message...")
|
||||
# socketio.send("lala")
|
||||
# print("send lala")
|
||||
# print(clients[0])
|
||||
# socketio.emit("server_event", {'msg': "tolle nachricht"}, room=clients[0])
|
||||
# with app.test_request_context('/'):
|
||||
# socketio.emit('server_event', "You!: server_event", namespace="/")
|
||||
# print("send bla")
|
||||
|
||||
socketio.run(app, host="localhost", port=5000, debug=True)
|
||||
# socketio.run(app, debug=True)
|
||||
# ENDE
|
||||
|
||||
print("runnung?!")
|
||||
print("running?!")
|
||||
# t.join()
|
||||
|
||||
Reference in New Issue
Block a user