2016-09-27 86 views
0

我正在尝试在Spring 4中使用websocket Java。我创建了一个服务器处理程序。 这里是我的代码Websocket Spring 4

import java.util.logging.Logger; 
 

 
import org.springframework.web.socket.CloseStatus; 
 
import org.springframework.web.socket.WebSocketMessage; 
 
import org.springframework.web.socket.WebSocketSession; 
 
import org.springframework.web.socket.handler.TextWebSocketHandler; 
 

 
public class ServerHandler extends TextWebSocketHandler{ 
 
\t 
 
\t private Logger logger = Logger.getLogger(this.getClass().getName()); 
 
\t 
 
\t @Override 
 
\t public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t logger.info("Connection clodes with websocket server: session id " + session.getId()); 
 
\t } 
 
\t 
 
\t @Override 
 
\t public void afterConnectionEstablished(WebSocketSession session) throws Exception { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t logger.info("Connected user with websocket server: session id " + session.getId()); 
 
\t } 
 
\t 
 
\t @Override 
 
\t public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t 
 
\t \t super.handleMessage(session, message); 
 
\t } 
 
\t 
 
\t @Override 
 
\t public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t super.handleTransportError(session, exception); 
 
\t } 
 
}

接下来,我创建了一个WebSocket的配置者。下面是代码

import org.springframework.context.annotation.Bean; 
 
import org.springframework.context.annotation.Configuration; 
 
import org.springframework.web.socket.config.annotation.EnableWebSocket; 
 
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; 
 
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; 
 

 
@Configuration 
 
@EnableWebSocket 
 
public class WebsocketConfig implements WebSocketConfigurer{ 
 

 
\t 
 
\t @Override 
 
\t public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { 
 
\t \t registry.addHandler(myHandler(), "/websocket").withSockJS(); 
 
\t \t 
 
\t } 
 
\t 
 
\t @Bean 
 
    public ServerHandler myHandler() { 
 
     return new ServerHandler(); 
 
    } 
 
\t 
 
}

在我的客户(JavaScript的),我试图连接到服务器

var sock = new SockJS('http://localhost:9999/websocket');

,当我试图执行客户端,我有404错误 问题在哪里?

+0

你有没有找到解决这个问题的办法? –

回答

0

将@Controller注释添加到WebsocketConfig类,以便它可以处理来自客户端的请求。

@EnableWebSocket 
@Controller 
public class WebsocketConfig implements WebSocketConfigurer{ 

并设置setAllowedOrigins()以避免与不同域连接时出错。

registry.addHandler(myHandler(), "/websocket").setAllowedOrigins("*").withSockJS(); 
+0

更新后,我得到了另一个错误:XMLHttpRequest无法加载http:// localhost:9999/websocket/info?t = 1474989961280。请求的资源上没有“Access-Control-Allow-Origin”标题。原因'http:// localhost:8081'因此不被允许访问。响应的HTTP状态代码为404. –

+0

我已经删除了WebsocketConfig中的@Configuration注释 –

+0

您是否按照我在上面的答案中给出的方式设置了setAllowedOrigins(“*”)? – abaghel