I'm trying to split the logic into different files, but it only works in main #1198
Closed
KlaydKladowayIgr
started this conversation in
General
Replies: 2 comments 13 replies
-
What do you mean by "it's not working"? The ws.py file is not imported anywhere, maybe that is the problem? Also the way you are mounting Socket.IO into app, and app into Socket.IO is likely not what you want. You have to decide which of the two ASGI apps are going to be in the front, and only mount the other one under it. |
Beta Was this translation helpful? Give feedback.
9 replies
-
I solved the problem, but now there is another problem. Here is the code: #main.py
import importlib, os, socketio
from fastapi import FastAPI
app = FastAPI()
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
for root, dirs, files in os.walk("ws"):
for file in files:
if file.split('_')[-1][:-3] == "handler":
module = importlib.import_module(root.replace("\\", ".") + '.' + file[:-3])
module.setup(sio)
print(sio.namespaces)
sio_app = socketio.ASGIApp(sio, app)
app.add_route("/socket.io/", route=sio_app, methods=["GET", "POST"])
app.add_websocket_route("/socket.io/", sio_app) #ws/test_handler.py
import socketio
class TestHandler(socketio.AsyncNamespace):
def on_connect(self, sid, environ):
pass
def on_disconnect(self, sid):
pass
async def on_test(self, sid):
print("test", sid)
def setup(sio):
sio.register_namespace(TestHandler("/test")) #ws/2/12_handler.py
import socketio
class TestNamespace(socketio.AsyncNamespace):
def on_connect(self, sid, environ):
print("Connect", sid)
def on_disconnect(self, sid):
print("disconnect", sid)
def setup(sio):
sio.register_namespace(TestNamespace("/")) As a result, print(sio.namespaces) outputs ['/'], but it should be ['/', '/test'] |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I am trying to split the logic from one file into several, here is an example of my code:
Beta Was this translation helpful? Give feedback.
All reactions