diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.en.md" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.en.md" new file mode 100644 index 0000000000000000000000000000000000000000..cc6f9236d2756402c36c4ef24453fa48c50c3ed4 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.en.md" @@ -0,0 +1,36 @@ +# 消息广播 + +#### Description +广播消息文字,下个版本迭代文件广播传输 + +#### Software Architecture +Software architecture description + +#### Installation + +1. xxxx +2. xxxx +3. xxxx + +#### Instructions + +1. xxxx +2. xxxx +3. xxxx + +#### Contribution + +1. Fork the repository +2. Create Feat_xxx branch +3. Commit your code +4. Create Pull Request + + +#### Gitee Feature + +1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md +2. Gitee blog [blog.gitee.com](https://blog.gitee.com) +3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) +4. The most valuable open source project [GVP](https://gitee.com/gvp) +5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) +6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.md" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..f804dace20fdf20845de65f3e308a96b4a7759be --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/README.md" @@ -0,0 +1,18 @@ +# 消息广播 + +![你可以得到](./img.png) + +#### 介绍 + +广播消息文字,下个版本迭代文件广播传输。区域网内传递消息。最终版本类似`QQ`聊天或`群`聊天。你可以在上班的时候找你的小伙伴快乐聊天。 + +#### 软件架构 +软件架构说明 +- python +- django + +#### 使用说明 + +- `pip install -r req` +- `python manage.py runserver 0.0.0.0:80` +- 访问`http://localhost/chat/index/`开始你们的聊天或传递消息 diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/img.png" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/img.png" new file mode 100644 index 0000000000000000000000000000000000000000..0fa495196729d7168f3f829c48df1b3c6f756455 Binary files /dev/null and "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/img.png" differ diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/__init__.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/__init__.py" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/asgi.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/asgi.py" new file mode 100644 index 0000000000000000000000000000000000000000..d7a2a9e3151ee50497ca37a79567f61d4ab5067e --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/asgi.py" @@ -0,0 +1,28 @@ +""" +ASGI config for instant project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ +""" + +import os +from django.core.asgi import get_asgi_application +from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.security.websocket import AllowedHostsOriginValidator + +import ws.routing + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'instant.settings') + +application = ProtocolTypeRouter( + { + "http": get_asgi_application(), + "websocket": AllowedHostsOriginValidator( + AuthMiddlewareStack(URLRouter(ws.routing.websocket_urlpatterns)) + ), + + } +) diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/settings.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/settings.py" new file mode 100644 index 0000000000000000000000000000000000000000..5c0e3090c6dcebd77741f255527574cf5f200ebe --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/settings.py" @@ -0,0 +1,125 @@ +""" +Django settings for instant project. + +Generated by 'django-admin startproject' using Django 4.2.5. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-2fcdi_q_+(%y)7*^6$(7y13juzcfhxhni7o9ll8omd@r*tcr#a' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + +# Application definition + +INSTALLED_APPS = [ + 'daphne', + 'ws', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + # 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'instant.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'instant.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +ASGI_APPLICATION = 'instant.asgi.application' +CHANNEL_LAYERS = { + "default": { + "BACKEND": "channels.layers.InMemoryChannelLayer" + } +} \ No newline at end of file diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/urls.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/urls.py" new file mode 100644 index 0000000000000000000000000000000000000000..3583488467f6c0b4ab95a8e4db4ccc02edb3e5a9 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/urls.py" @@ -0,0 +1,23 @@ +""" +URL configuration for instant project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path("chat/", include("ws.urls")), +] diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/wsgi.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/wsgi.py" new file mode 100644 index 0000000000000000000000000000000000000000..923e456c92dc453f8e1e41f3ff00c62c65a5c594 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/instant/wsgi.py" @@ -0,0 +1,16 @@ +""" +WSGI config for instant project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'instant.settings') + +application = get_wsgi_application() diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/manage.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/manage.py" new file mode 100755 index 0000000000000000000000000000000000000000..36b1ec5bf216ac30c030e497a50029d5c0127080 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/manage.py" @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'instant.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/req.txt" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/req.txt" new file mode 100644 index 0000000000000000000000000000000000000000..6bca0c54349770168ca9bc596fcd4769ccd3f609 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/req.txt" @@ -0,0 +1,24 @@ +asgiref==3.7.2 +attrs==23.1.0 +autobahn==23.6.2 +Automat==22.10.0 +cffi==1.15.1 +channels==4.0.0 +constantly==15.1.0 +cryptography==41.0.3 +daphne==4.0.0 +Django==4.2.5 +hyperlink==21.0.0 +idna==3.4 +incremental==22.10.0 +pyasn1==0.5.0 +pyasn1-modules==0.3.0 +pycparser==2.21 +pyOpenSSL==23.2.0 +service-identity==23.1.0 +six==1.16.0 +sqlparse==0.4.4 +Twisted==23.8.0 +txaio==23.1.1 +typing_extensions==4.8.0 +zope.interface==6.0 diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/__init__.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/__init__.py" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/admin.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/admin.py" new file mode 100644 index 0000000000000000000000000000000000000000..8c38f3f3dad51e4585f3984282c2a4bec5349c1e --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/admin.py" @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/apps.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/apps.py" new file mode 100644 index 0000000000000000000000000000000000000000..2c87a4821ad79e958c9cb2aee902ff15a88b8556 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/apps.py" @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class WsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'ws' diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/consumers.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/consumers.py" new file mode 100644 index 0000000000000000000000000000000000000000..b26e85875a780e97a87421139a5c5caee25568d2 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/consumers.py" @@ -0,0 +1,37 @@ +# @Author : hk +# @DateTime : 2023-09-18 13:20 +import json + +from channels.generic.websocket import AsyncWebsocketConsumer + + +class ChatConsumer(AsyncWebsocketConsumer): + async def connect(self): + self.room_name = self.scope["url_route"]["kwargs"]["room_name"] + self.room_group_name = "chat_%s" % self.room_name + + # Join room group + await self.channel_layer.group_add(self.room_group_name, self.channel_name) + + await self.accept() + + async def disconnect(self, close_code): + # Leave room group + await self.channel_layer.group_discard(self.room_group_name, self.channel_name) + + # Receive message from WebSocket + async def receive(self, text_data): + text_data_json = json.loads(text_data) + message = text_data_json["message"] + + # Send message to room group + await self.channel_layer.group_send( + self.room_group_name, {"type": "chat_message", "message": message} + ) + + # Receive message from room group + async def chat_message(self, event): + message = event["message"] + + # Send message to WebSocket + await self.send(text_data=json.dumps({"message": message})) diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/migrations/__init__.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/migrations/__init__.py" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/models.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/models.py" new file mode 100644 index 0000000000000000000000000000000000000000..71a836239075aa6e6e4ecb700e9c42c95c022d91 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/models.py" @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/routing.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/routing.py" new file mode 100644 index 0000000000000000000000000000000000000000..4fd89e5cc5ddf913ea4d1d430a395a15d60cd947 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/routing.py" @@ -0,0 +1,9 @@ +# @Author : hk +# @DateTime : 2023-09-18 13:29 +from django.urls import re_path + +from . import consumers + +websocket_urlpatterns = [ + re_path(r"ws/chat/(?P\w+)/$", consumers.ChatConsumer.as_asgi()), +] diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/index.html" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..e82623048c3d321cbe5f56d6220c2f153bc3b1a6 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/index.html" @@ -0,0 +1,27 @@ + + + + + + Chat Rooms + + + What chat room would you like to enter?
+
+ + + + + \ No newline at end of file diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/room.html" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/room.html" new file mode 100644 index 0000000000000000000000000000000000000000..3014a7b528840007f4b4d964f7fd4947e6c9a613 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/templates/room.html" @@ -0,0 +1,50 @@ + + + + + + Chat Room + + +
+
+ +{{ room_name|json_script:"room-name" }} + + + \ No newline at end of file diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/tests.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/tests.py" new file mode 100644 index 0000000000000000000000000000000000000000..e4defabc8afbec48d89189ef98a5e9697d1657d0 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/tests.py" @@ -0,0 +1 @@ +# from django.test import TestCase diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/urls.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/urls.py" new file mode 100644 index 0000000000000000000000000000000000000000..55eeba5936b1d15345326214cdaed5147bc72222 --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/urls.py" @@ -0,0 +1,11 @@ +# @Author : hk +# @DateTime : 2023-09-18 11:44 +from django.urls import path + +from . import views + +urlpatterns = [ + path("", views.index, name="index"), + path("/", views.room, name="room"), + path('upload/', views.upload, name='upload') +] diff --git "a/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/views.py" "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/views.py" new file mode 100644 index 0000000000000000000000000000000000000000..90ee4107c780a26a4df53053917922ce2fd7c25a --- /dev/null +++ "b/\346\266\210\346\201\257\345\271\277\346\222\255/dj-channels/instant/ws/views.py" @@ -0,0 +1,37 @@ +from django.shortcuts import render +from django.http import JsonResponse +from django.conf import settings +from pathlib import Path + +from datetime import datetime + + +# Create your views here. +def fm_datetime(): + now = datetime.now() + return now.strftime('%Y%m%d%H%M%S') + + +def index(request): + return render(request, "index.html") + + +def room(request, room_name): + return render(request, "room.html", {"room_name": room_name}) + + +def upload(request): + file = request.FILES.get('file') + folder = fm_datetime() + Path(folder).mkdir(exist_ok=True) + file_name = file.name + path = Path(settings.BASE_DIR).joinpath('file', folder, file_name) + if path.exists(): + file_name = '%s1%s' % (Path(file_name).name, Path(file_name).suffix) + file_path = ['file', folder, file_name] + with open(Path(settings.BASE_DIR).joinpath(*file_path)) as f: + f.write(file.read()) + return JsonResponse({'data': '/'.join(file_path)}) + + +