springboot websocket实现时遇到的No "user" header in message问题

银粟 发布于 2017/03/09 10:40
阅读 1K+
收藏 0

在使用springboot+websocket做单对单的通话时遇到了org.springframework.messaging.simp.annotation.support.MissingSessionUserException: No "user" header in message 这个问题,没有找到解决方法,希望大家帮助下,Thx!

我的websocket配置类:

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // TODO Auto-generated method stub
        registry.addEndpoint("/endpointChat").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // TODO Auto-generated method stub
        registry.enableSimpleBroker("/queue");
    }

    
}

我的Controller

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate mt;
    
    @MessageMapping("/chatd")
    public void handleChat(Principal principal,String msg){
        if(principal.getName().equals("a")){
            mt.convertAndSendToUser("cc", "/queue/notifications", principal.getName() + "-send:" + msg);
        }else{
            mt.convertAndSendToUser("a", "/queue/notifications", principal.getName() + "-send:" + msg);
        }
    }
}

我的页面

<!DOCTYPE html>
<html>
<head>
<title>Testing websockets</title>
<script type="text/javascript" src="../js/sockjs.min.js"></script>
<script type="text/javascript" src="../js/stomp.min.js"></script>
<script type="text/javascript" src="../js/jquery-3.1.1.min.js"></script>
</head>
<body>
 <form id="wiselyForm">
 <textarea rows="4" cols="60" name="text"></textarea>
 <input type="submit" value="提交"/>
 </form>
 <script>
 $("#wiselyForm").submit(function(e){
     e.preventDefault();
     var text = $("#wiselyForm").find('textarea[name="text"]').val();
     sendSpittle(text);
 });
 
 var sock = new SockJS("/endpointChat");
 var stomp = Stomp.over(sock);
 stomp.connect("guest","guest",function(frame){
     stomp.subscribe("/user/queue/notifications",handleNotification);
 });
 
 function handleNotification(message){
     $("#output").append("<b>Received:"+ message.body + "</b>");
 }
 
 function sendSpittle(text){
     stomp.send("/chatd",{},text);
 }
 $("#stop").click(function(){sock.close()});    
 
 </script>
 <div id="output"></div>
</body>
</html>

 

请大家帮忙看下,谢谢!

加载中
0
jack-hoo
jack-hoo

是你没有通过认证导致的

q
qwer112312
请问,因为跨域自定义了请求头,怎么能认证成功呢?
返回顶部
顶部