简体   繁体   中英

Apache Tomcat returning 404 when using websocket

I am using Apache Tomcat/8.0.47 on AWS Elastic Beanstalk and when I run on my local mac I can connect fine and get a good response with :

curl --include \
     --no-buffer \
     --header "Connection: Upgrade" \
     --header "Upgrade: websocket" \
     --header "Host: example.com:80" \
     --header "Origin: http://example.com:80" \
     --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
     --header "Sec-WebSocket-Version: 13" http://localhost:8080/echo

but if I ssh into the AWS instance I just get a 404.

This is not a nginx issue because I am running directly against the Tomcat port while ssh'd in.

I have spent the day looking around for a solution to this and I am at a loss. Anything I find online seems to be trying to solve load balancer/nginx issues.

for reference here is my code:

package com.mypackage.sockets

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

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

@ServerEndpoint(value = "/echo", configurator=EchoEndpointConfigurator.class)
public class EchoServer {

    private Set<Session> userSessions = Collections.synchronizedSet(new HashSet<Session>());

    @OnOpen
    public void onOpen(Session session) {
        System.out.println(session.getId() + " has opened a connection");
        userSessions.add(session);
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("Session " + session.getId() + " has ended");
        userSessions.remove(session);
    }

    @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();
        }
    }

}

and

package com.mypackage.sockets;

import javax.websocket.server.ServerEndpointConfig.Configurator;

public class EchoEndpointConfigurator extends Configurator {

    private static EchoServer echoServer = new EchoServer();

    @Override
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
        return (T)echoServer;
    }
}

I have a web.xml file too but that only references servlets which all work fine

Is the maybe some flag for Tomcat I might need to set on AWS Elastic Beanstalk

Turns out that despite the curl command working locally and not remotely this was not my problem. I was able to figure out that it was working on port 8080 by exposing that port directly on the ec2. In the end, to get it to work on port 80 I changed from Apache HTTPD to Nginx. Worked perfectly then using the classic Load Balancer. Have not figured out the Application Load-Balancer yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM