将Stomp服务注入JavaScript类

时间:2015-11-05 14:05:43

标签: javascript oop stompjs

我正在使用Stomp.js框架从Websocket发送和接收数据。 所以我必须创建一个类来帮助实现Stomp消息发送者。

然后我做了一个下课:

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;
    }
}

在课程中,我尝试使用以下方法与ActiveMQ建立连接:

var ws = Stomp.client( hostLocation, stompVersion );

Stomp对象存在于类中,但连接不起作用。

在HTML中我以这种方式实现:

<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>

有人知道我做错了吗?

聚苯乙烯。我导入了两个JavaScript文件。

谢谢!

0 个答案:

没有答案