gpt4 book ai didi

javascript - Spring启动WebSocket连接到 'ws://127.0.0.1:8081/getUsersList/'失败: Error during WebSocket handshake: Unexpected response code: 200

转载 作者:行者123 更新时间:2023-12-01 19:29:54 25 4
gpt4 key购买 nike

我的以下客户端抛出以下异常。

我也尝试过localhost

WebSocket connection to 'ws://127.0.0.1:8081/getUsersList/' failed: Error during WebSocket handshake: Unexpected response code: 200

当我尝试点击浏览器时,它会按预期传输数据,但是当我尝试从 Javascript 客户端获取数据时,就会导致问题。

http://127.0.0.1:8081/getUsersList

data:19:20:33
data:

data:19:20:34
data:

data:19:20:36
data:

data:19:20:37

我认为作为端口映射的一部分,我缺少一些东西,所以如果有人可以帮助解决这个问题,那就太好了。

提前致谢!

代码:

index.html - Client

      <script type = "text/javascript">
function WebSocketTest() {

if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");

// Let us open a web socket
var ws = new WebSocket("ws://127.0.0.1:8081/getUsersList/");

ws.onopen = function() {

// Web Socket is connected, send data using send()
//ws.send("Message to send");
// alert("Message is sent...");
};

ws.onmessage = function (evt) {
var received_msg = evt.data;
alert("Message is received...");
};

ws.onclose = function() {

// websocket is closed.
alert("Connection is closed...");
};
} else {

// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>

</head>

<body>
<div id = "sse">
<a href = "javascript:WebSocketTest()">Run WebSocket</a>
</div>

</body>
</html>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Webflux-POC</groupId>
<artifactId>Webflux-POC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Webflux-POC</name>
<description>POC on webflux</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>


<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>

<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.9</version>
</dependency>


<!-- https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api -->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>


</dependencies>


<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

WebfluxApp.java

package com.poc.webflux;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.reactive.config.EnableWebFlux;

@SpringBootApplication
@EnableWebFlux
@ComponentScan
public class WebfluxApp {
public static void main(String[] args) {
SpringApplication.run(WebfluxApp.class);
}
}

UsersController.java

package com.poc.webflux;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.poc.webflux.service.UserService;

import reactor.core.publisher.Flux;

@RestController
@CrossOrigin
public class UsersController {

@Autowired
private UserService userService;

@GetMapping(value="/getUsersList",produces = "text/event-stream")
public Flux<String> getUsersList(){
return userService.getUsersList();
}

}

UserService.java

package com.poc.webflux.service;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Objects;
import java.util.stream.Stream;

import org.springframework.stereotype.Service;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import reactor.core.publisher.Flux;

@Service
public class UserService {

public Flux<String> getUsersList() {
return Flux.fromStream(Stream.generate(() -> {
String str = getRandomNumber();
System.out.println(str);
return str ;
}))
//.log()
.delayElements(Duration.ofSeconds(1));

}

private String getRandomString() {
return "Test";
}
}

最佳答案

您正在尝试在 http 端点建立 Websocket 连接。要在响应式(Reactive)堆栈上公开 WebSocket 连接端点,您需要实现 WebSocketHandler 并提供到端点和已实现的 WebSocketHandler 的 HandlerMapping。

简历中:1 - 创建一个实现 WebSocketHandler 的类,并实现处理方法上的业务逻辑(在您的情况下是 userservice 的逻辑)。2 - 创建一个 @Configuration,它公开 HandlerMapping 的 bean,其中包含要建立 WebSocket 连接的端点和 WebSocketHandler 实现。

请参阅official documentation有关响应式(Reactive) Websocket 实现的更多详细信息。

关于javascript - Spring启动WebSocket连接到 'ws://127.0.0.1:8081/getUsersList/'失败: Error during WebSocket handshake: Unexpected response code: 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59268803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com