import importlib
import sys

import gevent
try:
    import geventwebsocket  # noqa
    _websocket_available = True
except ImportError:
    _websocket_available = False


class Thread(gevent.Greenlet):  # pragma: no cover
    """
    This wrapper class provides gevent Greenlet interface that is compatible
    with the standard library's Thread class.
    """
    def __init__(self, target, args=[], kwargs={}):
        super(Thread, self).__init__(target, *args, **kwargs)

    def _run(self):
        return self.run()


class WebSocketWSGI(object):  # pragma: no cover
    """
    This wrapper class provides a gevent WebSocket interface that is
    compatible with eventlet's implementation.
    """
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):

        self._sock = environ["wsgi.websocket"]
        self.environ = environ
        self.version = self._sock.version
        self.path = self._sock.path
        self.origin = self._sock.origin
        self.protocol = self._sock.protocol
        return self.app(self)

    def close(self):
        return self._sock.close()

    def send(self, message):
        return self._sock.send(message)

    def wait(self):
        return self._sock.receive()


async = {
    'threading': sys.modules[__name__],
    'thread_class': 'Thread',
    'queue': importlib.import_module('gevent.queue'),
    'queue_class': 'JoinableQueue',
    'websocket': sys.modules[__name__] if _websocket_available else None,
    'websocket_class': 'WebSocketWSGI' if _websocket_available else None
}

@@ -799,16 +799,276 @@
 ponse):%0A
+        if 'wsgi.websocket' not in environ:%0A            raise RuntimeError('You need to use the gevent-websocket server. '%0A                               'See the Deployment section of the '%0A                               'documentation for more information.')
 %0A       
@@ -1089,17 +1089,17 @@
 environ%5B
-%22
+'
 wsgi.web
@@ -1104,17 +1104,17 @@
 ebsocket
-%22
+'
 %5D%0A      

Raise a runtime error when gevent-websocket's custom server is not used