Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
asyncio.Task(redis_wait(subscriber, f))
# switch again
f.greenlet.parent.switch()
while True:
# any redis message in the queue ?
if f.done():
msg = f.result()
uwsgi.websocket_send("[%s] %s" % (time.time(), msg))
# restart coroutine
f = GreenFuture()
asyncio.Task(redis_wait(subscriber, f))
if myself.has_ws_msg:
myself.has_ws_msg = False
msg = uwsgi.websocket_recv_nb()
if msg:
asyncio.Task(redis_publish(connection, msg))
# switch again
f.greenlet.parent.switch()
<h1>WebSocket</h1>
<input id="testo" type="text">
<input value="invia" type="button">
<div style="width:640px;height:480px;background-color:black;color:white;border: solid 2px red;overflow:auto" id="blackboard">
</div>
""" % (ws_scheme, env['HTTP_HOST'])
elif env['PATH_INFO'] == '/foobar/':
uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', ''))
print "websockets..."
while True:
msg = uwsgi.websocket_recv_nb()
if msg:
queue.put(msg)
else:
try:
wait_read(uwsgi.connection_fd(), 0.1)
except gevent.socket.timeout:
try:
msg = queue.get_nowait()
uwsgi.websocket_send(msg)
except Exception:
pass
return ""
if fd == websocket_fd:
msg = uwsgi.websocket_recv_nb()
if msg:
r.publish('foobar', msg)
elif fd == redis_fd:
msg = channel.parse_response()
print(msg)
# only interested in user messages
t = 'message'
if sys.version_info[0] > 2:
t = b'message'
if msg[0] == t:
uwsgi.websocket_send("[%s] %s" % (time.time(), msg))
else:
# on timeout call websocket_recv_nb again to manage ping/pong
msg = uwsgi.websocket_recv_nb()
if msg:
r.publish('foobar', msg)
def keepalive():
"""Keep the websocket connection alive (called every 30 seconds)."""
print('PING/PONG...')
try:
uwsgi.websocket_recv_nb()
connection.add_timeout(30, keepalive)
except OSError as error:
print(error)
sys.exit(1) # Kill process and force uWSGI to Respawn
def keepalive():
"""Keep the websocket connection alive (called each minute)."""
print('PING/PONG...')
try:
uwsgi.websocket_recv_nb()
connection.add_timeout(30, keepalive)
except OSError as error:
print(error)
sys.exit(1) # Kill process and force uWSGI to Respawn
event_set = self._event.wait(timeout=3)
if event_set:
self._event.clear()
# maybe there is something to send
msgs = []
while True:
try:
msgs.append(self._send_queue.get(block=False))
except gevent.queue.Empty:
break
for msg in msgs:
self._send(msg)
# maybe there is something to receive, if not, at least
# ensure uWSGI does its ping/ponging
try:
msg = uwsgi.websocket_recv_nb()
except IOError: # connection closed
self._select_greenlet.kill()
return None
if msg: # message available
return self._decode_received(msg)
for fd in rlist:
if fd == channel_socket_fd:
message = channel.parse_response()
# See http://redis.io/topics/pubsub for format of `message`
if message[0] == 'message':
send_message('message', message[2])
elif fd == websocket_fd:
# Let uwsgi do it's job to receive pong and send ping
uwsgi.websocket_recv_nb()
else:
# Have not heard from channel and client in 5 seconds...
try:
# Check if the client is still here by sending ping
# (`websocket_recv` sends ping implicitly,
# `websocket_recv_nb` -- non-blocking variant of it)
uwsgi.websocket_recv_nb()
except IOError:
break
# Check if the build is still ongoing
if not r.exists(task_uuid):
send_message('status', 'finished')
break
return ''
def flush(self):
try:
uwsgi.websocket_recv_nb()
except IOError:
self.close()
def receive(self):
if self._closed:
raise WebSocketError("Connection is already closed")
try:
return uwsgi.websocket_recv_nb()
except IOError, e:
self.close()
raise WebSocketError(e)