2020-01-30 01:07:26 +00:00
|
|
|
from __future__ import absolute_import
|
2021-11-30 04:07:14 +00:00
|
|
|
import queue
|
2020-01-30 01:07:26 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
try:
|
2021-11-30 04:07:14 +00:00
|
|
|
from simple_websocket import Server, ConnectionClosed
|
|
|
|
_websocket_available = True
|
2020-01-30 01:07:26 +00:00
|
|
|
except ImportError: # pragma: no cover
|
2021-11-30 04:07:14 +00:00
|
|
|
_websocket_available = False
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketWSGI(object): # pragma: no cover
|
|
|
|
"""
|
|
|
|
This wrapper class provides a threading WebSocket interface that is
|
|
|
|
compatible with eventlet's implementation.
|
|
|
|
"""
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
self.ws = Server(environ)
|
|
|
|
return self.app(self)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
return self.ws.close()
|
|
|
|
|
|
|
|
def send(self, message):
|
|
|
|
try:
|
|
|
|
return self.ws.send(message)
|
|
|
|
except ConnectionClosed:
|
|
|
|
raise IOError()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
try:
|
|
|
|
return self.ws.receive()
|
|
|
|
except ConnectionClosed:
|
|
|
|
raise IOError()
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
_async = {
|
|
|
|
'thread': threading.Thread,
|
|
|
|
'queue': queue.Queue,
|
|
|
|
'queue_empty': queue.Empty,
|
|
|
|
'event': threading.Event,
|
2021-11-30 04:07:14 +00:00
|
|
|
'websocket': WebSocketWSGI if _websocket_available else None,
|
2020-01-30 01:07:26 +00:00
|
|
|
'sleep': time.sleep,
|
|
|
|
}
|