-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsumers.py
84 lines (70 loc) · 2.51 KB
/
consumers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from channels.generic.websocket import AsyncWebsocketConsumer, AsyncConsumer
from django.utils.decorators import method_decorator
from asgiref.sync import async_to_sync
from .api import Ln
from .decorators import postpone
from .models import Invoice
import uuid
import json
class LncConsumer(AsyncWebsocketConsumer):
async def connect(self):
#try:
# self.chan_hash = self.scope['session']['_auth_user_hash']
#except:
self.chan_hash = str(uuid.uuid4())
self.chan_hash = self.chan_name
await self.channel_layer.group_add(
self.chan_hash,
self.channel_name
)
await self.accept()
await self.channel_layer.group_send(self.chan_name, {
'type': 'chan_message',
'message': self.chan_name
})
async def receive(self, text_data=None, bytes_data=None):
try:
text_data = json.loads(text_data)
if text_data['message_type'] == "wait_invoice":
await self.channel_layer.send(
"lnws",
{
"type": "waitinvoice",
"group_id": self.chan_hash,
"payment_hash": text_data['payment_hash']
}
)
except:
pass
# Receive message from chan_hash group
async def chan_message(self, event):
message = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
async def disconnect(self, close_code):
# Leave group
try:
await self.channel_layer.group_discard(
self.self.chan_hash,
self.channel_name)
except:
pass
# use decorator only when using ./manage runserver
# @method_decorator(postpone, 'waitinvoice')
class WorkerConsumer(AsyncConsumer):
async def waitinvoice(self, message):
params = {}
params['payment_hash'] = message['payment_hash']
try:
result = Ln().invoice_wait(params)
if 'paid_at' in result.keys():
Invoice.objects.filter(rhash=params['payment_hash']).update(status=result['status'], pay_index=result['pay_index'])
await self.channel_layer.group_send(message['group_id'], {
'type': 'chan_message',
'message': "paid"
})
except:
# error or timeout
pass