Skip to content

Commit 8b25c6b

Browse files
author
Stefan Majoor
committed
xxx
1 parent 9e8bd20 commit 8b25c6b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

binder/websocket.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,24 @@ def list_rooms_for_user(self, user):
2929

3030
return rooms
3131

32+
channel = Noneq
33+
def get_websocket_channel():
34+
import pika
35+
from pika import BlockingConnection
36+
global channel
37+
if channel and channel.is_open:
38+
return channel
39+
connection_credentials = pika.PlainCredentials(settings.HIGH_TEMPLAR['rabbitmq']['username'],
40+
settings.HIGH_TEMPLAR['rabbitmq']['password'])
41+
connection_parameters = pika.ConnectionParameters(settings.HIGH_TEMPLAR['rabbitmq']['host'],
42+
credentials=connection_credentials)
43+
connection = BlockingConnection(parameters=connection_parameters)
44+
channel = connection.channel()
45+
return channel
3246

3347
def trigger(data, rooms):
3448
if 'rabbitmq' in getattr(settings, 'HIGH_TEMPLAR', {}):
35-
import pika
36-
from pika import BlockingConnection
37-
38-
connection_credentials = pika.PlainCredentials(settings.HIGH_TEMPLAR['rabbitmq']['username'],
39-
settings.HIGH_TEMPLAR['rabbitmq']['password'])
40-
connection_parameters = pika.ConnectionParameters(settings.HIGH_TEMPLAR['rabbitmq']['host'],
41-
credentials=connection_credentials)
42-
connection = BlockingConnection(parameters=connection_parameters)
43-
channel = connection.channel()
44-
49+
channel = get_websocket_channel()
4550
channel.basic_publish('hightemplar', routing_key='*', body=jsondumps({
4651
'data': data,
4752
'rooms': rooms,

0 commit comments

Comments
 (0)