Javax WebSockets 不适用于 programmattic Tomcat 7 实例化

标签 java tomcat websocket catalina

这是我正在尝试做的事情:

  • 以编程方式设置 Tomcat 7 服务器 - 可以使用简单的 servlet。我仅从代码中定义 Tomcat 的端点,文件系统上没有设置或路由器(这是一个约束)。
  • 让上述 Tomcat 服务器包含一个使用 Javax Websockets API (1.1) 的 websocket 服务器端点 - 这不起作用

我的应用程序/服务器入口点:

package com.myapp;

import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.deploy.FilterDef;
import org.apache.catalina.deploy.FilterMap;
import org.apache.catalina.startup.Tomcat;

public class App 
{
    public static void main(String[] args) throws LifecycleException
    {
        int port = 8080;

        Tomcat webServer = new Tomcat();

        webServer.setPort(port);
        webServer.setHostname("localhost");
        String appBase = ".";
        webServer.getHost().setAppBase(appBase);
        File docBase = new File(System.getProperty("java.io.tmpdir"));
        Context context = webServer.addContext("", docBase.getAbsolutePath());

        // both MyServlet and MyFilter exist and work.
        Class servletClass = MyServlet.class;

        Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName());
        context.addServletMapping("/my-servlet/*", servletClass.getSimpleName());

        Class filterClass = MyFilter.class;
        FilterDef myFilterDef = new FilterDef();
        myFilterDef.setFilterClass(filterClass.getName());
        myFilterDef.setFilterName(filterClass.getSimpleName());
        context.addFilterDef(myFilterDef);

        FilterMap myFilterMap = new FilterMap();
        myFilterMap.setFilterName(filterClass.getSimpleName());
        myFilterMap.addURLPattern("/my-servlet/*");
        context.addFilterMap(myFilterMap);

        webServer.start();
        webServer.getServer().await();
    }
}

我的 websocket 服务器端点类:

package com.myapp;

import java.nio.ByteBuffer;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/web-socket/")
public class WebSocket
{

    @OnOpen
    public void onOpen()
    {
        System.out.println("Open Connection ...");
    }

     @OnMessage
    public static  void onTextMessage(Session session, String msg) {
        System.out.println("On Message for Web Socket");
    }   
    @OnMessage
    public void onBinaryMessage(Session session, ByteBuffer msg){
        System.out.println("On Message for Web Socket");
    }   
    @OnMessage
    public void onPongMessage(Session session, PongMessage pMsg) {
        System.out.println("On Message for Web Socket");
    }
    @OnClose
    public void onClose(Session session) {
        System.out.println("Connection Close for Web Socket");
    }

    @OnError
    public void onError(Throwable e)
    {
        e.printStackTrace();
    }
}

我正在使用 Maven,我将 WebSocket API 包含在 <scope>provided</scope> 中,正如我在其他问题中看到的那样,这是其他问题的根源。

当我运行服务器时,我可以点击 my-servlet端点成功(当然作为网页),但是当我尝试创建 WS 对象时,只需在浏览器的开发控制台中使用 var webSocket = new WebSocket("ws://localhost:8080/web-socket") ,它说它不能到达终点:

WebSocket connection to 'ws://localhost:8080/web-socket' failed: Error during WebSocket handshake: Unexpected response code: 404

我的问题:我错过了什么?错误消息(来自 Chrome)表明端点未注册(因此出现 404)。如何注册 websocket ServerEndpoint?谢谢!

编辑:

这是我对 MyServlet 的实现类:

package com.myapp;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet
{

    @Override
    protected void doGet(
            HttpServletRequest req,
            HttpServletResponse resp) throws IOException
    {

        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().write("Works...");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

}

这是我对 MyFilter 的实现:

package com.myapp;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class MyFilter implements Filter
{

    @Override
    public void init(FilterConfig filterConfig)
    {
        // ...
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {

        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.addHeader("myHeader", "myHeaderValue");
        chain.doFilter(request, httpResponse);
    }

    @Override
    public void destroy()
    {
        // ...
    }
}

最佳答案

试试这个来手动添加网络套接字端点:


    String serverContainerClass = ServerContainer.class.getName();
    //should be "javax.websocket.server.ServerContainer", if not, some external package could have hogged the implementation
    final ServerContainer serverContainer = (ServerContainer) context.getServletContext().getAttribute(serverContainerClass);
    try 
    {
        serverContainer.addEndpoint(WebSocket.class);
    } 
    catch (DeploymentException e)
    {
        TraceWriter.Error(this, "Failed to initialize websocket", e);
    }

您应该在将 servlet 添加到上下文之后放置此代码

关于Javax WebSockets 不适用于 programmattic Tomcat 7 实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029190/

相关文章:

java - 为 ArrayList.contains() 实现 equals & hashcode 不工作

java - Firebase 数据库值未保存

java - 如何在 java 中均匀调整 JFrame 的大小以使其保持其方形

apache - 将客户端证书从 Apache 转发到 Tomcat

image - Golang Websocket 检测文件消息并在超过 1 个 ws 帧字节时写入

java - 为什么 Point 占用的内存比 Integer 少

java - 杀毒软件会降低基于 tomcat 的应用程序的速度

java - HelloWorld servlet : eclipse ee tomcat 6 error

java - Play framework 2.5.0 Websockets 示例

javascript - RxJS:Observable.webSocket() 获取 onopen、onclose ……