Spring 4 WebSocket应用程序

 qwer 发布于 2023-02-08 15:07

我试图从spring网站运行这个例子: 教程 除了Spring Boot部分.

在web.xml


    Archetype Created Web Application

    
        sample
        org.springframework.web.servlet.DispatcherServlet
        
            contextClass
            
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            
        
        
            contextConfigLocation
            
                com.evgeni.websock.WebSocketConfig
            
        
    

    
        sample
        /
    

Java配置:

@Configuration
@ComponentScan(basePackages = {"com.evgeni.controller"})
@EnableWebSocketMessageBroker
@EnableWebMvc
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketMessageBrokerConfigurer  {

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

    public void configureClientInboundChannel(ChannelRegistration registration) {
        // TODO Auto-generated method stub

    }

    public void configureClientOutboundChannel(ChannelRegistration registration) {
        // TODO Auto-generated method stub

    }

    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app"); 
    }
     @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
            registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
            registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
        }

}

控制器:

@Controller
public class GreetingController {


    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(3000); // simulated delay
        System.out.println(message.getName());
        return new Greeting("Hello, " + message.getName() + "!");
    }

}

的index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    Hello WebSocket
    
    
    



Everithing与教程相同,除了从web.xml加载conf和在jsp中加载2-3 c:url以添加项目的根.

当我点击连接然后发送时,在浏览器控制台中我得到:

Opening Web Socket... stomp.js:122
Web Socket Opened... stomp.js:122
>>> CONNECT
login:
passcode:
accept-version:1.1,1.0
heart-beat:10000,10000

 stomp.js:122
<<< ERROR
message:Illegal header\c 'login\c'. A header must be of the form \c
content-length:0

 stomp.js:122
>>> SEND
destination:/websock/app/hello
content-length:14

{"name":"asd"} 

我认为问题在于Sock js的连接功能

stompClient.connect('', '', function(frame) {...

我正在传递''用于登录和密码.

编辑: 当我将连接功能更改stompClient.connect('random', 'random',为控制台中的响应时:

Opening Web Socket... stomp.js:122
Web Socket Opened... stomp.js:122
>>> CONNECT
login:asd
passcode:asd
accept-version:1.1,1.0
heart-beat:10000,10000

 stomp.js:122
<<< CONNECTED
heart-beat:0,0
version:1.1

 stomp.js:122
connected to server undefined stomp.js:122
Connected: CONNECTED
version:1.1
heart-beat:0,0

 (index):23
>>> SUBSCRIBE
id:sub-0
destination:/websock/topic/greetings

 stomp.js:122
>>> SEND
destination:/websock/app/hello
content-length:14

{"name":"asd"} 

但是消息未传递给控制器​​.

1 个回答
  • 错误是错误的控制器映射.我有:

      @MessageMapping("/hello")
        @SendTo("/topic/greetings")
        public Greeting greeting(HelloMessage message) throws Exception
    

    在jsp中:

    stompClient.subscribe("<c:url value='/topic/greetings'/>", function(greeting){...
    

    stompClient.send("<c:url value='/app/hello'/>", {}, JSON.stringify({ 'name': name }));
    

    正确的是:

    stompClient.subscribe('/topic/greetings', function(greeting){...
    stompClient.send('/app/hello', {}, JSON.stringify({ 'name': name }));
    

    c:url添加项目的根,当我删除它时,应用程序工作.但是在这里使用SockJs创建新套接字时需要c:url(root):

    var socket = new SockJS("<c:url value='/hello'/>");
    

    2023-02-08 15:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有