2016-09-27 8 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

関連する問題