使用java WebSocket和spring boot的spring boot
我是SpringBoot新手,但在尝试使用SpringBoot运行JavaWebSocket应用程序时遇到了一个问题 我有这样一个Spring应用程序类:使用java WebSocket和spring boot的spring boot,java,spring,spring-boot,java-websocket,Java,Spring,Spring Boot,Java Websocket,我是SpringBoot新手,但在尝试使用SpringBoot运行JavaWebSocket应用程序时遇到了一个问题 我有这样一个Spring应用程序类: @SpringBootApplication public class HLACSSApplication { public static void main(String[] args) { SpringApplication.run(HLACSSApplication.class, args); }
@SpringBootApplication
public class HLACSSApplication {
public static void main(String[] args) {
SpringApplication.run(HLACSSApplication.class, args);
}
}
和一个Spring应用程序配置类:
@ComponentScan("com.test.hlacssmdw")
public class HLACSSApplicationConfig { }
下面是我创建web套接字服务器端点的方式:
package com.test.hlacssmdw.endpoints;
@ServerEndpoint("/ss")
public class HLACSSSocketServer {
@OnOpen
public void open(Session session){
System.out.println("In open()");
System.out.println("Session Related data : " + session.getId());
System.out.println("Out open()");
}
@OnMessage
public String handleMessage(String message, Session session) {
System.out.println("In handleMessage()");
System.out.println("Session Related data : " + session.getId());
System.out.println("Message Received is : " + message);
System.out.println("Out handleMessage()");
return "Message Received Successfully.";
}
@OnClose
public void close(Session session){
System.out.println("In close()");
System.out.println("Session Related data : " + session.getId());
System.out.println("Out close()");
}
}
我已经创建了一个网页,其中包含以下连接到此服务器端点的javascript,如下所示:
var socket = new WebSocket("ws://localhost:1112/ss");
socket.onmessage = onMessage;
function onMessage(event) {
console.log(event.data);
}
但我无法连接并在浏览器控制台上不断收到此错误:
到“ws://localhost:1112/ss”的WebSocket连接失败:过程中出错
WebSocket握手:意外响应代码:404
在服务器端,我收到以下错误:
org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:342)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:317)
at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:110)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1037)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1904)
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:94)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:450)
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:127)
我无法理解,因为当我不使用spring boot时,即当我使用maven创建一个简单的war时,同样的结构也在工作。我认为这是因为我在使用SpringBoot时没有war文件名,但我不确定
请帮忙 您是否将服务器端口设置为1112?默认值是8080。这个问题/答案似乎非常相关: