无法在tomcat 8上打开与websocket的连接

时间:2015-06-16 20:12:40

标签: java tomcat websocket

我有以下ServerEndpoint,它只不过是一个测试:

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/echo")
public class EchoService {

 /**
 * @OnOpen allows us to intercept the creation of a new session.
 * The session class allows us to send data to the user.
 * In the method onOpen, we'll let the user know that the handshake was 
 * successful.
 */
@OnOpen
public void onOpen(Session session){
    System.out.println(session.getId() + " has opened a connection"); 
    try {
        session.getBasicRemote().sendText("Connection Established");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

/**
 * When a user sends a message to the server, this method will intercept the message
 * and allow us to react to it. For now the message is read as a String.
 */
@OnMessage
public void onMessage(String message, Session session){
    System.out.println("Message from " + session.getId() + ": " + message);
    try {
        session.getBasicRemote().sendText(message);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

/**
 * The user closes the connection.
 * 
 * Note: you can't send messages to the client from this method
 */
@OnClose
public void onClose(Session session){
    System.out.println("Session " +session.getId()+" has ended");
}

}

现在我正在尝试使用Simple Websocket Client Chrome Extension连接到它,但它永远无法连接。

我认为这可能与我使用Tomcat 8作为servlet容器这一事实有关。我将此依赖项添加到我的pom.xml

<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
    <scope>provided</scope>
</dependency>

请注意,我将其设置为提供,因为tomcat 8附带websocket api .. 我想知道是否应该在我的web.xml中添加一些内容,但无法找到任何相关的内容..

我尝试连接的URI是ws:// localhost:8080 / ecommerce-view / echo,因为我的项目战被命名为ecommerce-view

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试使用public IEnumerable<TestCaseData> GetExtraWifQuestionsTestData() { yield return TestCaseData1(); yield return TestCaseData2(); } 。因此,请删除@ServerEndpoint("/echo")

相关问题