-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Reckless-Satoshi/asynchronous-chat
Minimal chat. Just enough for it to work. Does not log any message, so admin won't be able to resolve disputes easily with this set up.
- Loading branch information
Showing
24 changed files
with
533 additions
and
40 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
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ChatConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'chat' |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from api.logics import Logics | ||
from api.models import Order | ||
|
||
import json | ||
|
||
class ChatRoomConsumer(AsyncWebsocketConsumer): | ||
|
||
|
||
async def connect(self): | ||
self.order_id = self.scope['url_route']['kwargs']['order_id'] | ||
self.room_group_name = f'chat_order_{self.order_id}' | ||
self.user = self.scope["user"] | ||
self.user_nick = str(self.user) | ||
|
||
# Forbit if user is not part of the order | ||
# Does not work Async | ||
# order = Order.objects.get(id=self.order_id) | ||
|
||
# # Check if user is participant on the order. | ||
# if not (Logics.is_buyer(order[0], self.user) or Logics.is_seller(order[0], self.user)): | ||
# print ("Outta this chat") | ||
# return False | ||
|
||
print(self.user_nick) | ||
print(self.order_id) | ||
|
||
await self.channel_layer.group_add( | ||
self.room_group_name, | ||
self.channel_name | ||
) | ||
|
||
await self.accept() | ||
|
||
async def disconnect(self, close_code): | ||
await self.channel_layer.group_discard( | ||
self.room_group_name, | ||
self.channel_name | ||
) | ||
|
||
async def receive(self, text_data): | ||
text_data_json = json.loads(text_data) | ||
message = text_data_json['message'] | ||
nick = text_data_json['nick'] | ||
|
||
await self.channel_layer.group_send( | ||
self.room_group_name, | ||
{ | ||
'type': 'chatroom_message', | ||
'message': message, | ||
'nick': nick, | ||
} | ||
) | ||
|
||
async def chatroom_message(self, event): | ||
message = event['message'] | ||
nick = event['nick'] | ||
|
||
await self.send(text_data=json.dumps({ | ||
'message': message, | ||
'user_nick': nick, | ||
})) | ||
|
||
pass |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.urls import re_path | ||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
re_path(r'ws/chat/(?P<order_id>\w+)/$', consumers.ChatRoomConsumer.as_asgi()), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<html lang="en"> | ||
|
||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
|
||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" | ||
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> | ||
|
||
<title>Hello, world!</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="container"> | ||
<div class="row d-flex justify-content-center"> | ||
<div class="col-6"> | ||
<form> | ||
<div class="form-group"> | ||
<label for="exampleFormControlTextarea1" class="h4 pt-5">Chatroom</label> | ||
<textarea class="form-control" id="chat-text" rows="10"></textarea><br> | ||
</div> | ||
<div class="form-group"> | ||
<input class="form-control" id="input" type="text"></br> | ||
</div> | ||
<input class="btn btn-secondary btn-lg btn-block" id="submit" type="button" value="Send"> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{{ request.user.username|json_script:"user_username" }} | ||
{{ order_id|json_script:"order-id" }} | ||
|
||
<script> | ||
const user_username = JSON.parse(document.getElementById('user_username').textContent); | ||
document.querySelector('#submit').onclick = function (e) { | ||
const messageInputDom = document.querySelector('#input'); | ||
const message = messageInputDom.value; | ||
chatSocket.send(JSON.stringify({ | ||
'message': message, | ||
'username': user_username, | ||
})); | ||
messageInputDom.value = ''; | ||
}; | ||
|
||
|
||
|
||
|
||
const orderId = JSON.parse(document.getElementById('order-id').textContent); | ||
|
||
const chatSocket = new WebSocket( | ||
'ws://' + | ||
window.location.host + | ||
'/ws/chat/' + | ||
orderId + | ||
'/' | ||
); | ||
|
||
chatSocket.onmessage = function (e) { | ||
const data = JSON.parse(e.data); | ||
console.log(data) | ||
document.querySelector('#chat-text').value += (data.username + ': ' + data.message + '\n') | ||
} | ||
</script> | ||
|
||
<!-- Optional JavaScript --> | ||
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | ||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" | ||
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"> | ||
</script> | ||
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" | ||
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"> | ||
</script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" | ||
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"> | ||
</script> | ||
</body> | ||
|
||
</html> |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.index, name='index'), | ||
path('<str:order_id>/', views.room, name='order_chat'), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.shortcuts import render | ||
|
||
|
||
def index(request): | ||
return render(request, 'index.html', {}) | ||
|
||
def room(request, order_id): | ||
return render(request, 'chatroom.html', { | ||
'order_id': order_id | ||
}) |
Oops, something went wrong.