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

WebSocket consumers #2129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion channels/generic/websocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from json.decoder import JSONDecodeError

from asgiref.sync import async_to_sync

Expand All @@ -21,6 +22,7 @@ class WebsocketConsumer(SyncConsumer):
groups = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.groups is None:
self.groups = []

Expand Down Expand Up @@ -128,7 +130,12 @@ class JsonWebsocketConsumer(WebsocketConsumer):

def receive(self, text_data=None, bytes_data=None, **kwargs):
if text_data:
self.receive_json(self.decode_json(text_data), **kwargs)
try:
content = self.decode_json(text_data)
self.receive_json(content, **kwargs)
except JSONDecodeError as e:
raise ValueError(f"Invalid JSON format: {e}")

else:
raise ValueError("No text section for incoming WebSocket frame!")

Expand Down Expand Up @@ -162,6 +169,7 @@ class AsyncWebsocketConsumer(AsyncConsumer):
groups = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.groups is None:
self.groups = []

Expand Down