Тип WebFlux и WebSockets = Плохой запрос, статус = 400 - PullRequest
0 голосов
/ 14 марта 2019

Код DemoApplication

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public EchoHandler echoHandler() {
        return new EchoHandler();
    }

    @Bean
    public HandlerMapping handlerMapping() {
        Map<String, WebSocketHandler> map = new HashMap<>();
        map.put("/echo", echoHandler());
        var mapping = new SimpleUrlHandlerMapping();
        mapping.setUrlMap(map);
        mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return mapping;
    }

    @Bean
    public WebSocketHandlerAdapter handlerAdapter() {
        return new WebSocketHandlerAdapter(webSocketService());
    }

    @Bean
    public WebSocketService webSocketService() {
        var strategy = new ReactorNettyRequestUpgradeStrategy();
        return new HandshakeWebSocketService(strategy);
    }
}

EchoHandler

public class EchoHandler implements WebSocketHandler {
    @Override
    public Mono<Void> handle(WebSocketSession session) {
        return session
                .send(session.receive().map(msg -> "RECEIVED: " + msg.getPayloadAsText()).map(session::textMessage));
    }
}

app.js

var ws = null;
var url = "ws://localhost:8080/echo";
function setConnected(connected) {
    document.getElementById('connect').disabled = connected;
    document.getElementById('disconnect').disabled = !connected;
    document.getElementById('echo').disabled = !connected;
}
function connect() {
    ws = new WebSocket(url);
    ws.onopen = function() {
        setConnected(true);
    };
    ws.onmessage = function(event) {
        log(event.data);
    };
    ws.onclose = function(event) {
        setConnected(false);
        log('Info: Closing Connection.');
    };
}
function disconnect() {
    if (ws != null) {
        ws.close();
        ws = null;
    }
    setConnected(false);
}
function echo() {
    if (ws != null) {
        var message = document.getElementById('message').value;
        log('Sent: ' + message);
        ws.send(message);
    } else {
        alert('connection not established, please connect.');
    }
}
function log(message) {
    var console = document.getElementById('logging');
    var p = document.createElement('p');
    p.appendChild(document.createTextNode(message));
    console.appendChild(p);
    while (console.childNodes.length > 12) {
        console.removeChild(console.firstChild);
    }
    console.scrollTop = console.scrollHeight;
}

index.html

<body>
    <div>
        <div id="connect-container" class="ui centered grid">
            <div class="row">
                <button id="connect" onclick="connect();" class="ui green
button ">Connect</button>
                <button id="disconnect" disabled="disabled" onclick="disconnect();"
                    class="ui red button">Disconnect</button>
            </div>
            <div class="row">
                <textarea id="message" style="width: 350px" class="ui input"
                    placeholder="Message to Echo"></textarea>
            </div>
            <div class="row">
                <button id="echo" onclick="echo();" disabled="disabled"
                    class="ui button">Echo message</button>
            </div>
        </div>
        <div id="console-container">
            <h3>Logging</h3>
            <div id="logging"></div>
        </div>
    </div>
</body>

веб-страница

Пт. 15 марта 15:32:51 CST 2019

Произошла непредвиденная ошибка (тип = неверный запрос, статус = 400).

Недопустимый заголовок «Upgrade»: [Хост: «localhost: 8080», Соединение: «keep-alive», Назначение: «prefetch», Upgrade-Insecure-Requests: «1», User-Agent: «Mozilla / 5.0») (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, как Gecko) Chrome / 72.0.3626.119 Safari / 537.36 ", Accept:" text / html, application / xhtml + xml, application / xml; q = 0,9, изображение /webp,image/apng,/;q=0.8 ", Accept-Encoding:" gzip, deflate, br ", Accept-Language:" "]

Я изучаю весеннюю загрузку, но практика webflux и websockets может не работать. я не почему? Я посещаю localhost: 8080 / echo, отображение веб-страницы 400. Можете ли вы сказать мне, что я должен делать? спасибо.

...