|
2 | 2 |
|
3 | 3 | import json |
4 | 4 |
|
| 5 | +ALL_CONSUMERS = [] |
| 6 | + |
5 | 7 | class MessageConsumer(WebsocketConsumer): |
| 8 | + def __init__(self, *args, **kwargs): |
| 9 | + super(MessageConsumer, self).__init__(*args, **kwargs) |
| 10 | + global ALL_CONSUMERS |
| 11 | + ALL_CONSUMERS.append(self) |
6 | 12 |
|
7 | 13 | def connect(self): |
8 | 14 | self.accept() |
9 | 15 |
|
10 | 16 | def disconnect(self, close_code): |
11 | | - pass |
| 17 | + ac = [] |
| 18 | + global ALL_CONSUMERS |
| 19 | + for c in ALL_CONSUMERS: |
| 20 | + if c != self: |
| 21 | + ac.append(c) |
| 22 | + ALL_CONSUMERS = ac |
12 | 23 |
|
13 | 24 | def send_to_widgets(self, channel_name, label, value): |
14 | | - self.send(json.dumps({'channel_name':channel_name, |
| 25 | + message = json.dumps({'channel_name':channel_name, |
15 | 26 | 'label':label, |
16 | | - 'value':value})) |
| 27 | + 'value':value}) |
| 28 | + global ALL_CONSUMERS |
| 29 | + |
| 30 | + for c in ALL_CONSUMERS: |
| 31 | + c.send(message) |
| 32 | + |
17 | 33 | def receive(self, text_data): |
18 | | - print("Got incoming") |
19 | 34 | message = json.loads(text_data) |
20 | | - print(text_data) |
21 | | - self.send(json.dumps({'message':"Thanks for [%s]"%text_data})) |
22 | | - self.send(json.dumps({'original_message':message})) |
23 | 35 |
|
24 | | - # TODO if type is connection_triplet then store and/or update the info |
25 | | - # TODO else do something appropriate with the message |
| 36 | + message_type = message.get('type','unknown_type') |
| 37 | + |
| 38 | + if message_type == 'connection_triplet': |
| 39 | + |
| 40 | + channel_name = message.get('channel_name',"UNNAMED_CHANNEL") |
| 41 | + uid = message.get('uid',"0000-0000") |
| 42 | + label = message.get('label','DEFAULT$LABEL') |
| 43 | + |
| 44 | + # For now, send the uid as value. This essentially 'resets' the value |
| 45 | + # each time the periodic connection announcement is made |
| 46 | + self.send_to_widgets(channel_name=channel_name, |
| 47 | + label=label, |
| 48 | + value=uid) |
| 49 | + else: |
| 50 | + # Not a periodic control message, so do something useful |
| 51 | + # For now, this is just pushing to all other consumers indiscrimnately |
26 | 52 |
|
27 | | - channel_name = message.get('channel_name',"UNNAMED_CHANNEL") |
28 | | - uid = message.get('uid',"0000-0000") |
29 | | - label = message.get('label','DEFAULT$LABEL') |
| 53 | + channel_name = message.get('channel_name',"UNNAMED_CHANNEL") |
| 54 | + uid = message.get('uid',"0000-0000") |
| 55 | + value = message.get('value',{'source_uid':uid}) |
| 56 | + label = message.get('label','DEFAULT$LABEL') |
30 | 57 |
|
31 | | - self.send_to_widgets(channel_name=channel_name, |
32 | | - label=label, |
33 | | - value=uid) |
| 58 | + self.send_to_widgets(channel_name=channel_name, |
| 59 | + label=label, |
| 60 | + value=value) |
0 commit comments