Skip to content

Commit a6c34d4

Browse files
committed
Fix django_hx_chatserver
1 parent d7b44c2 commit a6c34d4

File tree

6 files changed

+99
-16
lines changed

6 files changed

+99
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
8+
[dev-packages]
9+
10+
[requires]
11+
python_version = "3.11"

examples/django_hx_chatserver/example_app/chat/templates/chat.html

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load staticfiles %}
1+
{% load static %}
22
<!DOCTYPE html>
33
<html lang="en">
44
<head>
@@ -100,15 +100,11 @@
100100

101101
<script type="text/javascript">
102102

103-
{%
104-
if address %}
103+
{% if address %}
105104
SOCKET_ADDRESS = '{{address}}';
106-
{% else %
107-
}
105+
{% else %}
108106
SOCKET_ADDRESS = null;
109-
{%
110-
endif %
111-
}
107+
{% endif %}
112108

113109
var setup_chat = function (sock) {
114110

@@ -176,4 +172,4 @@
176172

177173
</script>
178174

179-
</html>
175+
</html>

examples/django_hx_chatserver/example_app/example_app/settings.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@
4141
'chat', # your app goes here.
4242
)
4343

44-
MIDDLEWARE_CLASSES = (
44+
MIDDLEWARE = (
4545
'django.contrib.sessions.middleware.SessionMiddleware',
4646
'django.middleware.common.CommonMiddleware',
4747
'django.middleware.csrf.CsrfViewMiddleware',
4848
'django.contrib.auth.middleware.AuthenticationMiddleware',
49-
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
5049
'django.contrib.messages.middleware.MessageMiddleware',
5150
'django.middleware.clickjacking.XFrameOptionsMiddleware',
5251
)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from chat.views import home
22
from django.conf import settings
3-
from django.conf.urls import url
3+
from django.urls import re_path
44
from django.conf.urls.static import static
55
from django.contrib import admin
66

77
urlpatterns = [
8-
url(r'^(?P<chat_channel_name>\w+)$', home, name='home'),
9-
url(r'^$', home, name='home'),
10-
url(r'^admin/', admin.site.urls),
8+
re_path(r'^(?P<chat_channel_name>\w+)$', home, name='home'),
9+
re_path(r'^$', home, name='home'),
10+
re_path(r'^admin/', admin.site.urls),
1111
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

examples/django_hx_chatserver/example_app/run.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
deployer.add_non_tls_websocket_service(websocket_service)
88

99
deployer.run()
10-
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import json
2+
import uuid
3+
4+
from twisted.internet import threads
5+
from twisted.internet.protocol import Protocol
6+
7+
from hendrix.facilities.resources import NamedResource
8+
from .messaging import hxdispatcher
9+
10+
11+
def send_django_signal(transport, data):
12+
from .signals import message_signal
13+
message_signal.send(None, dispatcher=transport, data=data)
14+
15+
16+
class MessageHandlerProtocol(Protocol):
17+
"""
18+
A basic protocol for socket messaging
19+
using a hendrix messaging dispatcher to handle
20+
addressing messages to active sockets from
21+
different contexts
22+
"""
23+
dispatcher = hxdispatcher
24+
guid = None
25+
26+
def dataReceived(self, data):
27+
28+
"""
29+
Takes "data" which we assume is json encoded
30+
If data has a subject_id attribute, we pass that to the dispatcher
31+
as the subject_id so it will get carried through into any
32+
return communications and be identifiable to the client
33+
34+
falls back to just passing the message along...
35+
36+
"""
37+
try:
38+
address = self.guid
39+
data = json.loads(data)
40+
threads.deferToThread(send_signal, self.dispatcher, data)
41+
42+
if 'hx_subscribe' in data:
43+
return self.dispatcher.subscribe(self.transport, data)
44+
45+
if 'address' in data:
46+
address = data['address']
47+
else:
48+
address = self.guid
49+
50+
self.dispatcher.send(address, data)
51+
52+
except Exception as e:
53+
raise
54+
self.dispatcher.send(
55+
self.guid,
56+
{'message': data, 'error': str(e)}
57+
)
58+
59+
def connectionMade(self):
60+
"""
61+
establish the address of this new connection and add it to the list of
62+
sockets managed by the dispatcher
63+
64+
reply to the transport with a "setup_connection" notice
65+
containing the recipient's address for use by the client as a return
66+
address for future communications
67+
"""
68+
self.transport.uid = str(uuid.uuid1())
69+
70+
self.guid = self.dispatcher.add(self.transport)
71+
self.dispatcher.send(self.guid, {'setup_connection': self.guid})
72+
73+
def connectionLost(self, something):
74+
"clean up the no longer useful socket in the dispatcher"
75+
self.dispatcher.remove(self.transport)
76+
77+
78+
MessageResource = NamedResource('messages')

0 commit comments

Comments
 (0)