我对json,spring boot,websocket有疑问,请帮我

时间:2019-11-20 13:43:14

标签: javascript json websocket

我现在可以通过使用Web套接字按按钮来调用MySQL的表值。 首先,我完成了到WebSocket的连接。 这是WebSocket Config类。

@Configuration
@EnableWebSocketMessageBroker
public class AppConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry config) {
        config.addEndpoint("/socketrequest").withSockJS();
    }
}

这是控制器类

@RestController
@Controller
public class WebSocketController {
    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;
    // @Autowired
    // WebSocketService webSocketService;

    @MessageMapping("/processrequest")

    void runWebSocket(RequestData requestData) {
        new Thread(new RunProcess(requestData)).start();
    }

    private class RunProcess implements Runnable {
        private RequestData requestData;
        RunProcess(RequestData requestData) {
            this.requestData = requestData;
        }

        public void run() {
            simpMessagingTemplate.convertAndSend("/topic/response", requestData.getName());
            simpMessagingTemplate.convertAndSend("/topic/response", requestData.getFun());
            simpMessagingTemplate.convertAndSend("/topic/response", requestData.getPhone());
            simpMessagingTemplate.convertAndSend("/topic/response", requestData.getAge());
        }
    }
}

这是模型类

public class RequestData {
    private String name;
    private String fun;
    private String phone;
    private int age;

    public RequestData(String name, String fun, String phone, int age) {
        this.name = name;
        this.fun = fun;
        this.phone = phone;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFun() {
        return fun;
    }

    public void setFun(String fun) {
        this.fun = fun;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

最后一个index.html

<!DOCTYPE html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

        <script type="text/javascript">
            var stompClient = null;

            function connect() {
                var socket = new SockJS('http://localhost:8080/socketrequest');
                stompClient = Stomp.over(socket);
                stompClient.connect({}, function(frame) {
                    console.log('Connected: ' + frame);
                    stompClient.subscribe('/topic/response', function(message){
                        showOutput(message.body);
                    });
                });
            }


            function doWebsocket() {
                stompClient.send("/processrequest", {}, JSON.stringify({ name: "$(starter.name)", fun : "The second string", phone :  "third", age : 25}));
            }

            function doSse() {
                console.log("doSse");
                var rtUrl= '/emitter?string1=first string sse&string2=second string sse';
                var source = new EventSource(rtUrl);
                source.onmessage=function(event){
                    showOutput(event.data)
                };
            }

            function showOutput(message) {
                var response = document.getElementById('response');
                var p = document.createElement('p');
                p.style.wordWrap = 'break-word';
                p.appendChild(document.createTextNode(message));
                response.appendChild(p);
            }

            connect();
        </script>
    </head>

    <div>Starting page</div>
    <div>
        <button id="websocket" onclick="doWebsocket();">WebSocket</button>
    </div>
    <div>
        Response:
        <p id="response"></p>
    </div>
</html>

通过JSON.stringify在那里 当您按下名为WebSocket的按钮时,JSON格式在屏幕上输出文本。 现在,我想按下该按钮,以便获取mysql表的值。 我不知道该怎么解决。 我对很多我不知道的事情感到困惑,因为它太新了。 有谁可以帮助我吗?

0 个答案:

没有答案