无法打开与 tomcat 8 上的 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");
}
}

现在我正在尝试使用简单的Websocket客户端Chrome扩展程序连接到它,但它从未连接过。

我认为这可能与我使用 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。我想知道我是否应该在我的网站上添加一些东西.xml,但找不到任何相关的东西。

我尝试连接的 URI ws://localhost:8080/ecommerce-view/echo,因为我的项目战争被命名为电子商务视图

欢迎任何帮助。

尝试使用 @ServerEndpoint("/echo") .因此,请删除value=

相关内容

  • 没有找到相关文章

最新更新