Repository: django/channels

Issue:
[channels2] Consumer receive function
In attempting to get multiplexed websockets working (I believe I have it "almost working" :-) ), I noticed the following problem:

```
  File "/Users/brianmay/tree/myro/channels/channels/generic/websockets.py", line 40, in websocket_receive
    self.receive(text=message['text'])
  receive() missing 1 required positional argument: 'content'
```

In `WebsocketConsumer` we define receive as:

```
    def receive(self, text=None, bytes=None):                                    
```

(the use of the name bytes is unfortunate :-( - we replace the built-in Python type)

The problem being when override `WebsocketConsumer` with `JsonWebsocketConsumer` we also change the parameters:

```
    def receive(self, content, **kwargs):
```

The function call doesn't provide the required content parameter.

As it is, I think we need two functions, one that takes text/bytes, and the other one takes the JSON content.

Maybe the ``raw_receive()`` function was somehow intended to fix this?


Discussion:
The idea of `raw_receive` was that you could override `receive` and not have to re-implement other stuff, but I think we could do with some reworking around this stuff. I might say call the second one `receive_json`, and probably now is the time to not call it `bytes` too.
Any suggested alternatives to `bytes`? I am hopeless at naming things :-(
Well the underlying websocket concept is called a `binary frame`, but I didn't want to call it `binary` as that is perhaps even more misleading. Maybe `bytestring`? `byte_data` and `text_data`?
`byte_data` and `text_data` seem fine to me.
Maybe even `bytes_data`. That seems to flow better somehow.
Yes, agreed.

Resolving patch:
diff --git a/channels/generic/websockets.py b/channels/generic/websockets.py
--- a/channels/generic/websockets.py
+++ b/channels/generic/websockets.py
@@ -51,7 +51,7 @@ def send(self, text_data=None, bytes_data=None, close=False):
         """
         Sends a reply back down the WebSocket
         """
-        if text is not None:
+        if text_data is not None:
             super(WebsocketConsumer, self).send(
                 {"type": "websocket.send", "text": text_data},
             )
@@ -97,19 +97,19 @@ class JsonWebsocketConsumer(WebsocketConsumer):
     error on binary data.
     """
 
-    def raw_receive(self, message, **kwargs):
-        if "text" in message:
-            self.receive(self.decode_json(message['text']), **kwargs)
+    def receive(self, text_data=None, bytes_data=None, **kwargs):
+        if text_data:
+            self.receive_json(self.decode_json(text_data), **kwargs)
         else:
             raise ValueError("No text section for incoming WebSocket frame!")
 
-    def receive(self, content, **kwargs):
+    def receive_json(self, content, **kwargs):
         """
         Called with decoded JSON content.
         """
         pass
 
-    def send(self, content, close=False):
+    def send_json(self, content, close=False):
         """
         Encode the given content as JSON and send it to the client.
         """
@@ -191,7 +191,7 @@ class WebsocketDemultiplexer(JsonWebsocketConsumer):
     # Optionally use a custom multiplexer class
     multiplexer_class = WebsocketMultiplexer
 
-    def receive(self, content, **kwargs):
+    def receive_json(self, content, **kwargs):
         """Forward messages to all consumers."""
         # Check the frame looks good
         if isinstance(content, dict) and "stream" in content and "payload" in content:


Test patch: