I'm using the Stomp.js framework to send and receive data from a Websocket. So I have to make a class to help the implementation of the Stomp message sender.
Then I made a class bellow:
WebSocket.js
function WebSocket()
{
var host = { protocol: "ws://", url: "localhost", port: ":61623/" };
var hostLocation = hostToString(host);
var stompVersion = "v11.stomp";
var username = "admin";
var pass = "password";
this.sendMessage = function (paramMsg){
return wsConnection( hostLocation, stompVersion, paramMsg );
}
// make connection with ActiveMQ (WebSocket)
function wsConnection( hostLocation, stompVersion, paramMsg ) {
var ws = Stomp.client( hostLocation, stompVersion );
var subscribeTopic = "/topic/event";
ws.connect( username, pass,
function() {
ws.subscribe(subscribeTopic,
function( data ) {
return resultChannel(data);
},
{ priority: 9 }
);
ws.send(subscribeTopic, { priority: 9, text: paramMsg }, paramMsg);
}
);
}
// map the url connection
function hostToString(object) {
return object.protocol + object.url + object.port;
}
// format the result
function resultChannel(msg) {
console.log(msg.body);
return msg.body;
}
}
Inside the class I try to make a connection with the ActiveMQ using:
var ws = Stomp.client( hostLocation, stompVersion );
The Stomp object exist inside the class, but the connection not works.
In the HTML I implement that way:
<script type="text/javascript">
function sendMessage(){
var paramMsg = document.getElementById("mensagem").value;
var ws = new WebSocket();
ws.prototype = Stomp;
var ul = document.getElementById("textos");
ul.innerHTML += "<li>" + ws.sendMessage(paramMsg) + "</li>";
}
</script>
Someone know if I did something wrong?
Ps. I imported both JavaScript files.
Thank you!