I am trying to use django channels for the first time and i am following the tutorial in the documentation. But when I use python manage.py runserver and try to connect i get this error.
JavaScript
x
2
1
Protocol error, got "H" as reply type byte
2
Here is the whole console(I’m using anaconda):
JavaScript
1
30
30
1
Quit the server with CTRL-BREAK.
2
2018-06-20 15:59:25,665 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
3
2018-06-20 15:59:25,665 - INFO - server - Configuring endpoint tcp:port=8000:interface=127.0.0.1
4
2018-06-20 15:59:25,665 - INFO - server - Listening on TCP address 127.0.0.1:8000
5
[2018/06/20 15:59:36] HTTP GET /chat/lobby/ 200 [0.12, 127.0.0.1:62590]
6
[2018/06/20 15:59:36] WebSocket HANDSHAKING /ws/chat/lobby/ [127.0.0.1:62594]
7
2018-06-20 15:59:37,694 - ERROR - server - Exception inside application: Protocol error, got "H" as reply type byte
8
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelssessions.py", line 175, in __call__
9
return await self.inner(receive, self.send)
10
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelsmiddleware.py", line 41, in coroutine_call
11
await inner_instance(receive, send)
12
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelsconsumer.py", line 54, in __call__
13
await await_many_dispatch([receive, self.channel_receive], self.dispatch)
14
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelsutils.py", line 50, in await_many_dispatch
15
await dispatch(result)
16
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelsconsumer.py", line 67, in dispatch
17
await handler(message)
18
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannelsgenericwebsocket.py", line 173, in websocket_connect
19
await self.connect()
20
File "C:UsersPcDocumentstutorialdjango-channel-tutchatconsumers.py", line 11, in connect
21
self.channel_name
22
File "C:UsersPcAnaconda3envsdjangolibsite-packageschannels_rediscore.py", line 282, in group_add
23
channel,
24
File "C:UsersPcAnaconda3envsdjangolibsite-packagesaioredisconnection.py", line 181, in _read_data
25
obj = await self._reader.readobj()
26
File "C:UsersPcAnaconda3envsdjangolibsite-packagesaioredisstream.py", line 78, in readobj
27
obj = self._parser.gets()
28
Protocol error, got "H" as reply type byte
29
[2018/06/20 15:59:37] WebSocket DISCONNECT /ws/chat/lobby/ [127.0.0.1:62594]
30
On the frontend js return this error
JavaScript
1
3
1
(index):15 WebSocket connection to 'ws://127.0.0.1:8000/ws/chat/lobby/' failed: Error during WebSocket handshake: Unexpected response code: 500
2
(index):26 Chat socket closed unexpectedly
3
Here is the whole “pip freeze” list if it he
JavaScript
1
39
39
1
aioredis==1.1.0
2
asgiref==2.3.2
3
astroid==1.6.5
4
async-timeout==3.0.0
5
attrs==18.1.0
6
autobahn==18.6.1
7
Automat==0.7.0
8
certifi==2018.4.16
9
channels==2.1.2
10
channels-redis==2.2.1
11
colorama==0.3.9
12
constantly==15.1.0
13
daphne==2.2.0
14
Django==2.0.2
15
django-cors-headers==2.2.0
16
djangorestframework==3.8.2
17
hiredis==0.2.0
18
hyperlink==18.0.0
19
idna==2.7
20
incremental==17.5.0
21
isort==4.3.4
22
lazy-object-proxy==1.3.1
23
mccabe==0.6.1
24
msgpack==0.5.6
25
msgpack-python==0.5.6
26
olefile==0.45.1
27
Pillow==5.1.0
28
pylint==1.9.2
29
pypiwin32==223
30
pytz==2018.4
31
pywin32==223
32
redis==2.10.6
33
six==1.11.0
34
Twisted==18.4.0
35
txaio==2.10.0
36
wincertstore==0.2
37
wrapt==1.10.11
38
zope.interface==4.5.0
39
Here is the python code: consumer.py
JavaScript
1
40
40
1
from channels.generic.websocket import AsyncWebsocketConsumer
2
import json
3
4
class ChatConsumer(AsyncWebsocketConsumer):
5
async def connect(self):
6
self.room_name = self.scope['url_route']['kwargs']['room_name']
7
self.room_group_name = 'chat_%s' % self.room_name
8
9
await self.channel_layer.group_add(
10
self.room_group_name,
11
self.channel_name
12
)
13
14
self.accept()
15
16
async def disconnect(self, close_code):
17
await self.channel_layer.group_discard(
18
self.room_group_name,
19
self.channel_name
20
)
21
22
async def receive(self, text_data):
23
text_data_json = json.loads(text_data)
24
message = text_data_json['message']
25
26
await self.channel_layer.group_send(
27
self.room_group_name,
28
{
29
'type': 'chat_message',
30
'message': message
31
}
32
)
33
34
async def chat_message(self, event):
35
message = event['message']
36
37
await self.send(text_data=json.dumps({
38
'message': message
39
}))
40
routing.py
JavaScript
1
8
1
from django.conf.urls import url
2
3
from . import consumers
4
5
websocket_urlpatterns = [
6
url(r'^ws/chat/(?P<room_name>[^/]+)/$', consumers.ChatConsumer)
7
]
8
routing.py in mysite
JavaScript
1
14
14
1
from channels.routing import ProtocolTypeRouter, URLRouter
2
from channels.auth import AuthMiddlewareStack
3
4
import chat.routing
5
6
application = ProtocolTypeRouter({
7
# (http->django views is added by default)
8
'websocket': AuthMiddlewareStack(
9
URLRouter(
10
chat.routing.websocket_urlpatterns
11
)
12
),
13
})
14
part of settings.py
JavaScript
1
10
10
1
ASGI_APPLICATION = 'mysite.routing.application'
2
CHANNEL_LAYERS = {
3
'default': {
4
'BACKEND': 'channels_redis.core.RedisChannelLayer',
5
'CONFIG': {
6
"hosts": [('127.0.0.1', 8000)],
7
},
8
},
9
}
10
Javascript code
JavaScript
1
33
33
1
var roomName = {{ room_name_json }};
2
3
var chatSocket = new WebSocket(
4
'ws://' + window.location.host +
5
'/ws/chat/' + roomName + '/');
6
7
chatSocket.onmessage = function(e) {
8
var data = JSON.parse(e.data);
9
var message = data['message'];
10
document.querySelector('#chat-log').value += (message + 'n');
11
};
12
13
chatSocket.onclose = function(e) {
14
console.error('Chat socket closed unexpectedly');
15
};
16
17
document.querySelector('#chat-message-input').focus();
18
document.querySelector('#chat-message-input').onkeyup = function(e) {
19
if (e.keyCode === 13) { // enter, return
20
document.querySelector('#chat-message-submit').click();
21
}
22
};
23
24
document.querySelector('#chat-message-submit').onclick = function(e) {
25
var messageInputDom = document.querySelector('#chat-message-input');
26
var message = messageInputDom.value;
27
chatSocket.send(JSON.stringify({
28
'message': message
29
}));
30
31
messageInputDom.value = '';
32
};
33
Thanks for the help
Advertisement
Answer
Is there any chance you did not have a redis server running on your machine when running your django server? On a mac I did brew install redis
and then redis-server
and then ran my django server on another terminal window and all worked fine.