Spring boot websockets จัดการข้อความ STOMP

ฉันมีเซิร์ฟเวอร์ Spring Boot WebSocket และนี่คือการกำหนดค่าโบรกเกอร์ของฉัน

WebSocketConfig.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

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

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

และตัวควบคุมข้อความ

MessageController.java

@Controller
public class MessageController {

    @MessageMapping("/topic")
    @SendTo("/topic/greetings/{message}")
    public String handleMessage(@PathVariable String message){
        return "[" + message + "] at " + LocalDate.now();
    }
}

จาวาสคริปต์ของฉันเพื่อจัดการไคลเอนต์ websocket ด้วย

app.js

var stompClient = null;

function connect() {
    var socket = new SockJS('/endpoint');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings/asd', function (message){
            console.log("Message received: " + message)
        });
    });
}

function setConnected(connected) {
    $("#connect").prop("disabled", connected);
    $("#disconnect").prop("disabled", !connected);
    $("#greetings").html("");
}

function disconnect() {
    if (stompClient !== null) {
        stompClient.disconnect();
    }
    setConnected(false);
    console.log("Disconnected");
}

function sendName() {
    stompClient.send("/app/topic", {}, $("#name").val());
}

$(function () {
    $("form").on('submit', function (e) {
        e.preventDefault();
    });
    $( "#connect" ).click(function() { connect(); });
    $( "#disconnect" ).click(function() { disconnect(); });
    $( "#send" ).click(function() { sendName(); });
});

และหน้า html

ดัชนี.html

<!DOCTYPE html>
<html>
<head>
    <title>Hello WebSocket</title>
    <link href="/th/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/th/main.css" rel="stylesheet">
    <script src="/webjars/jquery/jquery.min.js"></script>
    <script src="/webjars/sockjs-client/sockjs.min.js"></script>
    <script src="/webjars/stomp-websocket/stomp.min.js"></script>
    <script src="/app.js"></script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
    enabled. Please enable
    Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">
    <div class="row">
        <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="connect">WebSocket connection:</label>
                    <button id="connect" class="btn btn-default" type="submit">Connect</button>
                    <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
                    </button>
                </div>
            </form>
        </div>
        <div class="col-md-6">
            <form class="form-inline">
                <div class="form-group">
                    <label for="name">What is your name?</label>
                    <input type="text" id="name" class="form-control" placeholder="Your name here...">
                </div>
                <button id="send" class="btn btn-default" type="submit">Send</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

ดังนั้นทุกข้อความที่มาที่ /app/topic ควรส่งไปที่ /topic/gretings/{message} แต่ไม่ใช่ ฉันมีข้อผิดพลาด:

java.lang.IllegalArgumentException: Could not resolve placeholder 'message' in value "/topic/greetings/{message}".

ฉันได้อ่านบทความอื่นๆ แล้วและผู้คนใช้ @DestinationVariable แทน @PathVariable แต่แล้วฉันก็พบข้อผิดพลาดอื่น:

org.springframework.messaging.MessageHandlingException: Missing path template variable 'message' for method parameter type [class java.lang.String].

ประเด็นนี้คือให้ลูกค้าสมัครรับข้อมูลช่องของตนเองและแลกเปลี่ยนข้อมูลกับลูกค้ารายอื่นซึ่งจะเป็นแอปพลิเคชันเดสก์ท็อปใน C#


person kszulchs    schedule 29.06.2018    source แหล่งที่มา


คำตอบ (1)


เมื่อคุณส่งข้อความในส่วนเนื้อหาด้วย stompClient.send("/app/topic", {}, $("#name").val()); คุณควรใช้ @RequestBody แทน @PathVariable

วิธีรับเนื้อหาของข้อความที่โพสต์:

@MessageMapping("/topic")
public String handleMessage(@RequestBody String message){

ถัดไปเพื่อส่งหลังจากการเสริมคุณค่าไปยังหัวข้อ /topic/greetings คุณสามารถใช้:

@SendTo("/topic/greetings")

เมื่อรวมทุกอย่างเข้าด้วยกันจะได้รับข้อความจาก /app/topic และส่งถึงสมาชิกของ /topic/greetings

@MessageMapping("/topic")
@SendTo("/topic/greetings")
public String handleMessage(@RequestBody String message){
    return "[" + message + "] at " + LocalDate.now();
}

หากคุณต้องการใช้ @PathVariable คุณควร map ส่งข้อความด้วย:

stompClient.send("/app/topic/" + $("#name").val() , {}, {});

และทำให้มันกำหนดการแมปเช่น:

@MessageMapping("/topic/{message}")
@SendTo("/topic/greetings")
public String handleMessage(@PathVariable String message){
person mpromonet    schedule 03.03.2019