-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deactivate websockets for production testing
- Loading branch information
1 parent
dce6c01
commit 331c58c
Showing
5 changed files
with
95 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import os | ||
from channels.layers import get_channel_layer | ||
from channels.routing import get_default_application | ||
# import os | ||
# from channels.layers import get_channel_layer | ||
# from channels.routing import get_default_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', | ||
'projexbackend.config.development') | ||
channel_layer = get_channel_layer() | ||
application = get_default_application() | ||
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', | ||
# 'projexbackend.config.development') | ||
# channel_layer = get_channel_layer() | ||
# application = get_default_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
from channels.generic.websocket import JsonWebsocketConsumer | ||
from rest_framework import serializers | ||
from api.serializers import * | ||
from asgiref.sync import async_to_sync | ||
import json | ||
# from channels.generic.websocket import JsonWebsocketConsumer | ||
# from rest_framework import serializers | ||
# from api.serializers import * | ||
# from asgiref.sync import async_to_sync | ||
# import json | ||
|
||
|
||
class NotificationConsumer(JsonWebsocketConsumer): | ||
def connect(self): | ||
# We're always going to accept the connection, though we may | ||
# close it later based on other factors. | ||
user = self.scope.get('user') | ||
group_name = user.get_group_name | ||
# class NotificationConsumer(JsonWebsocketConsumer): | ||
# def connect(self): | ||
# # We're always going to accept the connection, though we may | ||
# # close it later based on other factors. | ||
# user = self.scope.get('user') | ||
# group_name = user.get_group_name | ||
|
||
async_to_sync(self.channel_layer.group_add( | ||
group_name, | ||
self.channel_name, | ||
)) | ||
# async_to_sync(self.channel_layer.group_add( | ||
# group_name, | ||
# self.channel_name, | ||
# )) | ||
|
||
self.accept() | ||
# self.accept() | ||
|
||
def notify(self, event): | ||
""" | ||
This handles calls elsewhere in this codebase that look | ||
like: | ||
# def notify(self, event): | ||
# """ | ||
# This handles calls elsewhere in this codebase that look | ||
# like: | ||
|
||
channel_layer.group_send(group_name, { | ||
'type': 'notify', # This routes it to this handler. | ||
'content': json_message, | ||
}) | ||
# channel_layer.group_send(group_name, { | ||
# 'type': 'notify', # This routes it to this handler. | ||
# 'content': json_message, | ||
# }) | ||
|
||
Don't try to directly use send_json or anything; this | ||
decoupling will help you as things grow. | ||
""" | ||
# Don't try to directly use send_json or anything; this | ||
# decoupling will help you as things grow. | ||
# """ | ||
|
||
self.send_json(event["payload"]) | ||
# self.send_json(event["payload"]) | ||
|
||
def websocket_receive(self, content, **kwargs): | ||
""" | ||
This handles data sent over the wire from the client. | ||
# def websocket_receive(self, content, **kwargs): | ||
# """ | ||
# This handles data sent over the wire from the client. | ||
|
||
We need to validate that the received data is of the correct | ||
form. You can do this with a simple DRF serializer. | ||
# We need to validate that the received data is of the correct | ||
# form. You can do this with a simple DRF serializer. | ||
|
||
We then need to use that validated data to confirm that the | ||
requesting user (available in self.scope["user"] because of | ||
the use of channels.auth.AuthMiddlewareStack in routing) is | ||
allowed to subscribe to the requested object. | ||
""" | ||
#print(content) | ||
# serializer = self.get_serializer(data=content) | ||
# if not serializer.is_valid(): | ||
# return | ||
# # Define this method on your serializer: | ||
# group_name = serializer.get_group_name() | ||
# # The AsyncJsonWebsocketConsumer parent class has a | ||
# # self.groups list already. It uses it in cleanup. | ||
# self.groups.append(group_name) | ||
# # This actually subscribes the requesting socket to the | ||
# # named group: | ||
# await self.channel_layer.group_add( | ||
# group_name, | ||
# self.channel_name, | ||
# ) | ||
# We then need to use that validated data to confirm that the | ||
# requesting user (available in self.scope["user"] because of | ||
# the use of channels.auth.AuthMiddlewareStack in routing) is | ||
# allowed to subscribe to the requested object. | ||
# """ | ||
# #print(content) | ||
# # serializer = self.get_serializer(data=content) | ||
# # if not serializer.is_valid(): | ||
# # return | ||
# # # Define this method on your serializer: | ||
# # group_name = serializer.get_group_name() | ||
# # # The AsyncJsonWebsocketConsumer parent class has a | ||
# # # self.groups list already. It uses it in cleanup. | ||
# # self.groups.append(group_name) | ||
# # # This actually subscribes the requesting socket to the | ||
# # # named group: | ||
# # await self.channel_layer.group_add( | ||
# # group_name, | ||
# # self.channel_name, | ||
# # ) | ||
|
||
def websocket_disconnect(self): | ||
super(self) | ||
# def websocket_disconnect(self): | ||
# super(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
# mysite/routing.py | ||
from channels.routing import ProtocolTypeRouter | ||
from django.urls import path | ||
# # mysite/routing.py | ||
# from channels.routing import ProtocolTypeRouter | ||
# from django.urls import path | ||
|
||
from channels.auth import AuthMiddlewareStack | ||
from channels.routing import ProtocolTypeRouter, URLRouter | ||
# from channels.auth import AuthMiddlewareStack | ||
# from channels.routing import ProtocolTypeRouter, URLRouter | ||
|
||
from .consumers import NotificationConsumer | ||
# from .consumers import NotificationConsumer | ||
|
||
|
||
websockets = URLRouter([ | ||
path( | ||
"ws/notifications", | ||
NotificationConsumer, | ||
name="ws_notifications", | ||
) | ||
]) | ||
# websockets = URLRouter([ | ||
# path( | ||
# "ws/notifications", | ||
# NotificationConsumer, | ||
# name="ws_notifications", | ||
# ) | ||
# ]) | ||
|
||
|
||
application = ProtocolTypeRouter({ | ||
# (http->django views is added by default) | ||
"websocket": AuthMiddlewareStack(websockets), | ||
}) | ||
# application = ProtocolTypeRouter({ | ||
# # (http->django views is added by default) | ||
# "websocket": AuthMiddlewareStack(websockets), | ||
# }) |