2013-07-19 61 views
-1

我使用Chrome,它總是返回:如何讓spring + cometd支持webscoket?

WebSocket connection to 'ws://192.168.1.228:9090/achat/cometd' failed: Unexpected response code: 400 

此錯誤後,它使用長輪詢傳輸,併成功連接到服務器。

我該如何讓WebSocket工作?它是由使用SpringFramework 3.1引起的嗎?謝謝。

的web.xml

<!-- Filter to support cross domain requests --> 
<filter> 
    <filter-name>cross-origin</filter-name> 
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class> 
    <!--async-supported>true</async-supported--> 
</filter> 
<filter-mapping> 
    <filter-name>cross-origin</filter-name> 
    <url-pattern>/cometd/*</url-pattern> 
</filter-mapping> 
<!-- Cometd Servlet --> 
<servlet> 
    <servlet-name>cometd</servlet-name> 
    <servlet-class>org.cometd.annotation.AnnotationCometdServlet</servlet-class> 
    <!--async-supported>true</async-supported--> 
    <init-param> 
     <param-name>timeout</param-name> 
     <param-value>20000</param-value> 
    </init-param> 
    <init-param> 
     <param-name>interval</param-name> 
     <param-value>0</param-value> 
    </init-param> 
    <init-param> 
     <param-name>maxInterval</param-name> 
     <param-value>10000</param-value> 
    </init-param> 
    <init-param> 
     <param-name>maxLazyTimeout</param-name> 
     <param-value>5000</param-value> 
    </init-param> 
    <init-param> 
     <param-name>long-polling.multiSessionInterval</param-name> 
     <param-value>2000</param-value> 
    </init-param> 
    <init-param> 
     <param-name>logLevel</param-name> 
     <param-value>100</param-value> 
    </init-param> 
    <init-param> 
     <param-name>transports</param-name> 
     <param-value>org.cometd.websocket.server.WebSocketTransport</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>cometd</servlet-name> 
    <url-pattern>/cometd/*</url-pattern> 
</servlet-mapping> 

的Servlet

@Named 
@Singleton 
@Service 
public class ChatService { 
    @Inject 
    private BayeuxServer bayeux; 
    @Session 
    private ServerSession serverSession; 

    @Listener("/chat") 
    public void processHello(ServerSession remote, ServerMessage.Mutable message) 
    { 
     Map<String, Object> input = message.getDataAsMap(); 
     String room = input.get("room").toString(); 
     String content = input.get("content").toString(); 

     Map<String, Object> output = input;   
     bayeux.getChannel("/room/" + room).publish(remote, output, null); 
    } 
} 

回答

0

按照CometD Reference Manual,具體解釋如何整合CometD with Spring部分。

在那裏您可以找到一個使用Spring的XML配置來設置WebSocket的示例,以及如何通過註釋配置Spring的示例。

您的問題缺少Spring配置文件和web.xml文件的一部分,因此很難以更詳細的方式幫助您。

請記住,如果您在Spring中配置CometD,則必須從web.xml中刪除所有CometD配置。你只能在一個地方配置東西:web.xml或Spring的XML /註釋。