django=4.0.0
channel=3.0.4
First, please do not post screenshots of code or data. Copy/paste the text into your post, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.
We’re going to need more details.
- What does your asgi.py file look like?
- What does your routing.py file look like? (Or whatever other file you may have defining websocket urls - if any)
- How are you running this?
from channels.generic.websocket import WebsocketConsumer
class MyConsumer(WebsocketConsumer):
groups = ["broadcast"]
def connect(self):
self.accept()
self.accept("subprotocol")
self.close()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Hello world!")
self.send(bytes_data="Hello world!")
self.close()
self.close(code=4123)
def disconnect(self, close_code):
pass
import os
from .consumer import MyConsumer
from django.urls import path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter([path('ws/chat/',MyConsumer.as_asgi())]))
),
}
)
Why are you calling close
in your connect function? Why do you have two calls to accept
?
Have you worked your way through the official Channels tutorial to the point where you understand what it’s doing?