how can i make spring websocket + node.js client

[亡魂溺海] 提交于 2021-02-11 14:02:29

问题


how can i create a websocket stomp client (WebSocketStompClient) in node.js like i do in java with this lines..

WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    StompSession stompSession = stompClient.connect("ws://localhost:8081/stomp", new StompSessionHandlerAdapter() {
    }).get(10, SECONDS);

i´ve tried several websocket projects in node.js, but none of them work


回答1:


i get it!. this work fine with Spring boot websocket + stomp + StockJS

var Stomp = require('stompjs')
var SockJS = require('sockjs-client')
var url = 'http://localhost:8081/stomp';
console.log('connecting to '+url);
var sock= new SockJS(url);
var stompClient = Stomp.over(sock);


var callback = function(message){
  console.log('received message: '+message);
};


sock.onopen = function() {
  console.log('open');

};

sock.onmessage = function(e) {
  console.log('message', e.data);
  sock.close();
};

sock.onclose = function() {
  console.log('close');
};


stompClient.connect({},function (frame) {
  stompClient.subscribe('/user/queue/reply', callback);
  stompClient.send('/app/chat/este/es/el/chat/java/*/java/*',{},'{"name":"from nodejs"}');
});


来源:https://stackoverflow.com/questions/54786323/how-can-i-make-spring-websocket-node-js-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!