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

Rejected websocket connections throw an error in Tornado #114

Closed
miguelgrinberg opened this issue Jun 3, 2019 · 1 comment
Closed

Rejected websocket connections throw an error in Tornado #114

miguelgrinberg opened this issue Jun 3, 2019 · 1 comment

Comments

@miguelgrinberg
Copy link
Owner

socketio_1        | 2019-06-01 21:35:13,880.880 app:1   INFO    101 GET /socket.io/?token=3edcc438-0baf-4d1b-b0cb-3cfd5fe82d9a&EIO=3&transport=websocket (172.16.148.77) 1.04ms
socketio_1        | 2019-06-01 21:35:13,880.880 asyncio_pubsub_manager:1        INFO    aioredis backend initialized.
socketio_1        | 2019-06-01 21:35:13,881.881 asyncio_server:1        WARNING Application rejected connection
socketio_1        | 2019-06-01 21:35:13,882.882 app:1   ERROR   Task exception was never retrieved
socketio_1        | future: <Task finished coro=<AsyncServer.handle_request() done, defined at /usr/local/lib/python3.7/site-packages/engineio/asyncio_server.py:169> exception=RuntimeError('Method not supported for Web Sockets')>
socketio_1        | Traceback (most recent call last):
socketio_1        |   File "/usr/local/lib/python3.7/site-packages/engineio/asyncio_server.py", line 275, in handle_request
socketio_1        |     r['response'], environ)
socketio_1        |   File "/usr/local/lib/python3.7/site-packages/engineio/async_drivers/tornado.py", line 138, in make_response
socketio_1        |     tornado_handler.set_status(int(status.split()[0]))
socketio_1        |   File "/usr/local/lib/python3.7/site-packages/tornado/websocket.py", line 627, in _raise_not_supported_for_websockets
socketio_1        |     raise RuntimeError("Method not supported for Web Sockets")
socketio_1        | RuntimeError: Method not supported for Web Sockets

websocket does not support using set_status

using 3.7.0

Originally posted by @DeoLeung in #104 (comment)

@miguelgrinberg
Copy link
Owner Author

import asyncio
import logging

import socketio
import uvloop
from socketio import AsyncNamespace
from tornado import httpserver
from tornado import web

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())


class Default(AsyncNamespace):

  async def on_connect(self, sid: str, env: dict):
    logging.debug('login')
    # here we reject the connection
    return False

  def on_disconnect(self, sid):
    logging.debug('logout')

  async def trigger_event(self, event, *args):
    """log all events"""
    logging.debug('%s %s', event, args)
    return await super().trigger_event(event, *args)


def make_app() -> web.Application:
  _loop = asyncio.get_event_loop()
  sio = socketio.AsyncServer(async_mode='tornado')
  sio.register_namespace(Default(namespace='/'))
  router = [('/socket.io/', socketio.get_tornado_handler(sio))]
  app = web.Application(router, debug=True)
  return app


if __name__ == "__main__":
  logging.basicConfig(level=logging.DEBUG)
  tornado_app = make_app()
  loop = asyncio.get_event_loop()
  http_server = httpserver.HTTPServer(tornado_app)
  http_server.listen(8890)

  try:
    loop.run_forever()
  finally:
    loop.close()
# client
import logging
import time

from socketio import Client

logging.basicConfig(level=logging.DEBUG)

sio = Client()
sio.connect('http://localhost:8890', transports='websocket')

happens oh tornado 5.1.1 and 6.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant