Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes binary data sending #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions flask_uwsgi_websocket/_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@

class GeventWebSocketClient(object):
def __init__(self, environ, fd, send_event, send_queue, recv_event,
recv_queue, timeout=5):
recv_queue, is_binary, timeout=6000):
self.environ = environ
self.fd = fd
self.send_event = send_event
self.send_queue = send_queue
self.recv_event = recv_event
self.recv_queue = recv_queue
self.timeout = timeout
self.timeout = 6000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not hardcode the timeout here :)

self.id = str(uuid.uuid1())
self.connected = True
self.is_binary = is_binary

def send(self, msg, binary=True):
if binary:
return self.send_binary(msg)
#self.is_binary.clear()
self.send_queue.put(msg)
self.send_event.set()

def send_binary(self, msg):
self.is_binary.set()
self.send_queue.put(msg)
self.send_event.set()

Expand Down Expand Up @@ -68,10 +71,11 @@ def __call__(self, environ, start_response):

recv_event = Event()
recv_queue = Queue()
is_binary = Event()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not clear why a boolean will not work here.


# create websocket client
client = self.client(environ, uwsgi.connection_fd(), send_event,
send_queue, recv_event, recv_queue,
send_queue, recv_event, recv_queue, is_binary,
self.websocket.timeout)

# spawn handler
Expand All @@ -81,6 +85,7 @@ def __call__(self, environ, start_response):
def listener(client):
# wait max `client.timeout` seconds to allow ping to be sent
select([client.fd], [], [], client.timeout)
print('we received a message')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary logging.

recv_event.set()
listening = spawn(listener, client)

Expand All @@ -98,7 +103,10 @@ def listener(client):
if send_event.is_set():
try:
while True:
uwsgi.websocket_send(send_queue.get_nowait())
if not is_binary.is_set() or not is_binary:
uwsgi.websocket_send(send_queue.get_nowait())
else:
uwsgi.websocket_send_binary(send_queue.get_nowait())
except Empty:
send_event.clear()
except IOError:
Expand All @@ -119,7 +127,12 @@ def listener(client):
# should be able to ignore it anyway.
while message:
message = uwsgi.websocket_recv_nb()
recv_queue.put(message)
if message != b'':
recv_queue.put_nowait(message)

else:
message = False
listening.kill()
listening = spawn(listener, client)
except IOError:
client.connected = False
Expand Down