STOMP Messages - jordy33/turbogears_tutorial GitHub Wiki
Update setup.py add repository stomp.py in install_requires
"stomp.py",
Edit helpers.py
def stompServer():
return app_globals.stompServer
def stompUser():
return app_globals.stompUserName
def stompPassword():
return app_globals.stompPassword
Edit /lib/app_globals.py and insert in the class Globals in Init
# STOMP parameters
self.stomp_secure=os.getenv('STOMP_SECURE')
if self.stomp_secure is None:
self.stomp_secure="True"
if self.stomp_secure=="False":
stomp_prefix="ws://"
else:
stomp_prefix="wss://"
stomp_port=os.getenv('STOMP_PORT')
if stomp_port is None:
stomp_port=""
if len(stomp_port)>1:
self.stomp_port=":"+stomp_port
else:
self.stomp_port=""
self.stompHost = os.getenv('STOMP_HOST')
if self.stompHost is None:
self.stompHost = "stomp.dudewhereismy.mx"
self.stompUserName = os.getenv('STOMP_MASTER_USER')
if self.stompUserName is None:
self.stompUserName = "dwim"
self.stompPassword = os.getenv('STOMP_MASTER_PASS')
if self.stompPassword is None:
self.stompPassword = "gpscontrol1"
self.stompServer=stomp_prefix+self.stompHost+':15671'+'/ws'
In the lib directory add the file: messagequeue.py and paste the following:
from tg import app_globals,abort,request
import stomp
from stomp import ConnectionListener
from stomp import StatsListener
import logging
class MyListener(ConnectionListener):
def on_message(self, headers, message):
pass
class MyStatsListener(StatsListener):
def on_disconnected(self):
pass
class Message(object):
@classmethod
def post(cls, user,body):
"""Return Status of post"""
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
})
c = stomp.Connection([(app_globals.stompHost, 61613)])
c.start()
c.set_listener('my_listener', MyListener())
c.set_listener('stats_listener', MyStatsListener())
c.connect(app_globals.stompUserName , app_globals.stompPassword, wait=True)
c.send(body=body, destination='/topic/' + user )
c.disconnect()
return ""
Create messages.mak
<style type="text/css">
</style>
<script>
// STOMP
var subscribe ="/topic/testlistener_"+"${user}";
var message_test_client = Stomp.client('${h.stompServer()}');
message_test_client.debug=null;
var message_test_connect_callback = function() {
message_test_client.subscribe(subscribe, message_test_subscribe_callback);
// called back after the client is connected and authenticated to the STOMP server
};
var message_test_error_callback = function(error) {
alert(error);
};
var message_test_subscribe_callback = function(message) {
var msg = message.body;
var payload = msg.split('|');
var command = payload[0];
var data = payload[1];
switch (command) {
case 'RELOAD':
$('#jqGridAlerts').trigger( 'reloadGrid' );
break;
case 'MSG_GREEN':
$.alert(data, { autoClose:false,type: 'success',});
break;
case 'MSG_RED':
$.alert(data, { autoClose:false,type: 'danger',});
break;
case 'MSG_YELLOW':
$.alert(data, { autoClose:false,type: 'warning',});
break;
case 'MSG_BLUE':
$.alert(data, { autoClose:false,type: 'info',});
break;
case 'MSG_BLUE_WITH_AUTOCLOSE':
$.alert(data, { autoClose:true,type: 'info',});
break;
}
};
var stompUser='${h.stompUser()}';
var stompPass='${h.stompPassword()}';
message_test_client.connect(stompUser, stompPass, message_test_connect_callback, message_test_error_callback, '/');
function myFunction(color) {
$.ajax({
type: "GET",
url: '${h.url()}/test/receiveMessage/',
contentType: "application/json; charset=utf-8",
data: { 'color':color},
success: function(data) {
},
error: function() {
$.alert("Error accessing tables/addPriority", { autoClose:false,});
assignDialog.dialog( "close" );
addCallerEventDialog.dialog( "close" );
return true;
},
complete: function() {
}
});
}
function myFunction2() {
}
</script>
</div>
<!-- page start-->
<!-- Hidden POP UP Start-->
<!-- Hidden POP UP End-->
<div id="noteContent" title="${_('Basic dialog')}">
</div>
<!-- JQGRID table start-->
<h3>Send message test</h3>
<div class="dropdown">
<button onclick="myFunction('GREEN')" class="dropbtn">Verde</button>
<button onclick="myFunction('RED')" class="dropbtn">Rojo</button>
<button onclick="myFunction('YELLOW')" class="dropbtn">Amarillo</button>
<button onclick="myFunction('BLUE')" class="dropbtn">Azul</button>
<button onclick="myFunction('BLUE_WITH_AUTOCLOSE')" class="dropbtn">Azul Cerrando</button>
</div>
Create a route test and insert the following
@expose('pythonmercuryms.templates.test.messages')
def messages(self, **kw):
return dict(user=request.identity["repoze.who.userid"])
@expose('json')
def receiveMessage(self, **kw):
userName = request.identity['repoze.who.userid']
Message.post("testlistener_" + userName, 'MSG_'+kw['color'] + "|" + "Test")
return dict(dummy="")
add a Route in master.mak
<button onclick="openTab('Messages','${h.url()}/test/messages')" title="home of earth">Messages</button>