fanout / django-eventstream

Server-Sent Events for Django
MIT License
650 stars 85 forks source link

Exception inside application: Object of type 'dict_keys' is not JSON serializable #20

Closed LihengGong closed 5 years ago

LihengGong commented 5 years ago

Error message:

[2018/09/01 01:13:11] HTTP GET /messages/default/ 200 [0.08, 127.0.0.1:61023]
in EventsConsumer.py line 147: request= <AsgiRequest: GET '/events/?channel=room-default'>
in eventresponse.py line 73: es_meta= {'iss': 'es', 'exp': 1535767991, 'channels': dict_keys(['room-default']), 'user': '2'}
^^^^^^^^^ 
   >>>wrong<<<

2018-09-01 01:13:12,302 - ERROR - server - Exception inside application: Object of type 'dict_keys' is not JSON serializable
  File "venv/lib/python3.6/site-packages/channels/sessions.py", line 175, in __call__
    return await self.inner(receive, self.send)
  File "venv/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
    await inner_instance(receive, send)
  File "venv/lib/python3.6/site-packages/channels/generic/http.py", line 26, in __call__
    await self.handle(b"".join(body))
  File "venv/lib/python3.6/site-packages/django_eventstream/consumers.py", line 148, in handle
    response = event_response.to_http_response(request)
  File "venv/lib/python3.6/site-packages/django_eventstream/eventresponse.py", line 74, in to_http_response
    params['es-meta'] = jwt.encode(es_meta, settings.SECRET_KEY.encode('utf-8'))
  File "venv/lib/python3.6/site-packages/jwt/api_jwt.py", line 62, in encode
    cls=json_encoder
  File "/anaconda/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/anaconda/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/anaconda/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/anaconda/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
  Object of type 'dict_keys' is not JSON serializable
[2018/09/01 01:13:12] HTTP GET /events/?channel=room-default 500 [0.90, 127.0.0.1:61023]

Envrionment:

(venv) ➜ pip freeze asgiref==2.3.2 async-timeout==3.0.0 attrs==18.1.0 autobahn==18.8.1 Automat==0.7.0 certifi==2018.8.24 channels==2.1.3 chardet==3.0.4 constantly==15.1.0 daphne==2.2.2 Django==2.1 django-eventstream==2.2.0 django-grip==1.8.0 djangorestframework==3.8.2 gripcontrol==3.3.0 hyperlink==18.0.0 idna==2.7 incremental==17.5.0 psycopg2-binary==2.7.5 pubcontrol==2.4.2 PyHamcrest==1.9.0 PyJWT==1.6.4 pytz==2018.5 requests==2.19.1 six==1.11.0 Twisted==18.7.0 txaio==18.7.1 urllib3==1.23 Werkzeug==0.14.1 zope.interface==4.5.0

Client side code:

conn_eventstream: function(){
                  let uri = '/events/?channel=room-' + encodeURIComponent('{{ room_name }}');
                  let es = new ReconnectingEventSource(uri);
                  console.log('stream opened. room_name=' + uri);

                  es.addEventListener('open', function () {
                      console.log('Event stream opened')
                  }, false);

                  es.addEventListener('error', function () {
                      console.log('Error. Connection lost. Trying to reconnect')
                  }, false);

                  es.addEventListener('message', function (evt) {
                      console.log(('Message from server: ' + evt.data))
                  }, false)
              }
          }
jkarneges commented 5 years ago

Argh, I fixed this in git but hadn't made a release. This is done now, as 2.2.1. Sorry about that.