I have a flask-socketio server that serves index that shows received messages on webpage. The server receives messages trough ZMQ or basic UDP in their own threads. In the same threads it emits the messages to webpage but only the ZMQ thread message are received. Can you tell me why doesn't the UDP thread eimtting work?
from flask import Flask, request
from flask_socketio import SocketIO, emit
from threading import Thread
import socket
import time, zmq, pmt
HTTP_PORT = 5000
ZMQ_PORT = 5001
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
app = Flask(__name__, static_url_path="")
# app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app)
def background_thread():
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
message = 'hello'
socketio.emit('gnu radio', (message,))
time.sleep(0.10)
print "received message:", data
def background_thread_2():
# Establish ZMQ context and socket
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
socket.connect("tcp://0.0.0.0:%d" % (ZMQ_PORT))
while True:
# Receive decoded ADS-B message from the decoder over ZMQ
pdu_bin = socket.recv()
pdu = str(pmt.deserialize_str(pdu_bin)).decode('utf-8', 'ignore').encode("utf-8")
message = 'hello2'
socketio.emit('gnu radio', (message,))
time.sleep(0.10)
@app.route("/")
def index():
return app.send_static_file("index.html")
@socketio.on("connect")
def connect():
print("Client connected", request.sid)
@socketio.on("disconnect")
def disconnect():
print("Client disconnected", request.sid)
if __name__ == "__main__":
thread = Thread(target=background_thread)
thread.daemon = True
thread.start()
thread = Thread(target=background_thread_2)
thread.daemon = True
thread.start()
socketio.run(app, host="0.0.0.0", port=HTTP_PORT, debug=True)
Only Hello2 is received.