【问题标题】:How to use websocket in spring boot and react js?如何在spring boot中使用websocket并响应js?
【发布时间】:2020-04-04 22:16:03
【问题描述】:

我参考了这个spring boot doc 在 Spring Boot 中创建 Web 套接字。

我用过stompjssockjs-client

var socket = new SockJS('/gs-guide-websocket');
//gives an error in below line
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('/topic/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

【问题讨论】:

    标签: spring-boot react-native websocket


    【解决方案1】:

    使用这个stompjssocket-client

    然后导入它们

    import SockJS from 'sockjs-client';
    import Stomp from 'stompjs';
    

    连接websocket

    //use your link here
    var sock = new SockJS('http://localhost:8102/api/ws');
    let stompClient = Stomp.over(sock);
    sock.onopen = function() {
      console.log('open');
    }
    stompClient.connect({}, function (frame) {
       console.log('Connected: ' + frame);
       stompClient.subscribe('/topic/public', function (greeting) {
         console.log(greeting);
         //you can execute any function here
       });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2021-11-03
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 2016-10-20
      • 2018-07-31
      相关资源
      最近更新 更多