Spring mvc 如何使用spring+;在服务器上获取websocket响应;跺脚+;袜子?
在我的javascript中,我创建了一个websocket:Spring mvc 如何使用spring+;在服务器上获取websocket响应;跺脚+;袜子?,spring-mvc,controller,websocket,stomp,sockjs,Spring Mvc,Controller,Websocket,Stomp,Sockjs,在我的javascript中,我创建了一个websocket: <script type="text/javascript"> /// Create stomp client over sockJS protocol (see Note 1) var socket = new SockJS("http://localhost:8080/hello"); var stompClient = Stomp.over(socket); // callback functio
<script type="text/javascript">
/// Create stomp client over sockJS protocol (see Note 1)
var socket = new SockJS("http://localhost:8080/hello");
var stompClient = Stomp.over(socket);
// callback function to be called when stomp client is connected to server (see Note 2)
var connectCallback = function() {
alert("connected!");
stompClient.subscribe('http://localhost:8080/topic/greetings', function(greeting){
alert(JSON.parse(greeting.body).content);
});
};
// callback function to be called when stomp client could not connect to server (see Note 3)
var errorCallback = function(error) {
// display the error's message header:
alert(error.headers.toString);
};
// Connect as guest (Note 4)
stompClient.connect("guest", "guest", connectCallback, errorCallback);
</script>
spring配置的一部分:
@Controller
public class MessageController {
@RequestMapping("/test")
public ModelAndView getBodyPage() throws SQLException {
ModelAndView modelAndView = new ModelAndView("testWebSocket");
return modelAndView;
}
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(String name) throws Exception {
System.out.println(name);//here!
return new Greeting("Hello, " + name + "!");
}
}
<!-- WebSocket -->
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
看到您的代码时,会有一些误解:
http://localhost:8080/topic/greetings
您应该订阅
/topic/问候语
testWebSocket.jsp
)中,您将向
/hello
但是没有这种目的地的映射,您应该
将其发送到/app/hello
,因为/app
是为应用程序配置的
目的地(这些消息将由您的控制器处理)http://localhost:8080/hello
(可能缺少应用程序名称或servlet映射?)查看查看您的代码时存在一些误解:
http://localhost:8080/topic/greetings
您应该订阅
/topic/问候语
testWebSocket.jsp
)中,您将向
/hello
但是没有这种目的地的映射,您应该
将其发送到/app/hello
,因为/app
是为应用程序配置的
目的地(这些消息将由您的控制器处理)http://localhost:8080/hello
(可能缺少应用程序名称或servlet映射?)查看查看您的代码时存在一些误解:
http://localhost:8080/topic/greetings
您应该订阅
/topic/问候语
testWebSocket.jsp
)中,您将向
/hello
但是没有这种目的地的映射,您应该
将其发送到/app/hello
,因为/app
是为应用程序配置的
目的地(这些消息将由您的控制器处理)http://localhost:8080/hello
(可能缺少应用程序名称或servlet映射?)查看查看您的代码时存在一些误解:
http://localhost:8080/topic/greetings
您应该订阅
/topic/问候语
testWebSocket.jsp
)中,您将向
/hello
但是没有这种目的地的映射,您应该
将其发送到/app/hello
,因为/app
是为应用程序配置的
目的地(这些消息将由您的控制器处理)http://localhost:8080/hello
(可能缺少应用程序名称或servlet映射?)检查非常感谢您的回答,我编辑了以上所有内容,但当我尝试连接时,浏览器控制台会显示下一条消息:WebSocket连接到“ws://localhost:8080/hello”失败:WebSocket握手时出错:意外响应代码:200非常感谢您的回答,我编辑了以上所有内容,但当我尝试连接时,在浏览器控制台中出现下一条消息:WebSocket连接到“ws://localhost:8080/hello”失败:WebSocket握手时出错:意外响应代码:200非常感谢您的回答,我编辑了以上所有内容,但当我尝试连接时,在浏览器控制台中出现下一条消息:WebSocket连接到“ws://localhost:8080/hello”失败:WebSocket握手时出错:意外响应代码:200非常感谢您的回答,我编辑了以上所有内容,但当我尝试连接时,在浏览器控制台中出现下一条消息:WebSocket连接到“ws://localhost:8080/hello”失败:WebSocket握手期间出错:意外响应代码:200
<!-- WebSocket -->
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>