我怎样才能使Spring Websocket + Node.js客户端

时间:2019-02-20 12:23:02

标签: node.js spring-websocket

我如何在node.js中创建websocket踩踏客户端(WebSocketStompClient),就像我在Java中用这行代码所做的那样。.

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

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

我已经在node.js中尝试了多个websocket项目,但是它们都不起作用

1 个答案:

答案 0 :(得分:0)

我明白了!使用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"}');
});