1

ちょっと遊んでみるためにアンダーツーをセットアップしようとしています。私は静的なhtmlファイルindex.htmlをサーバー化し、注釈付きのwebsocket ServerEndpoint pojoを追加しようとしているので、次のことを行いました

final Xnio xnio = Xnio.getInstance("nio", Undertow.class.getClassLoader());
    final XnioWorker xnioWorker = xnio.createWorker(OptionMap.builder().getMap());

    WebSocketDeploymentInfo webSockets  = new WebSocketDeploymentInfo ()
           .addEndpoint(Socket.class)
           .setWorker(xnioWorker);

    final DeploymentManager deployment = defaultContainer()
            .addDeployment(deployment()
                    .setClassLoader(main.class.getClassLoader())
                    .setContextPath("/websockets")
                    .setDeploymentName("websockets")
                    .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, webSockets));

    deployment.deploy();
    deployment.start();


    Undertow.builder()
            .addHttpListener(8000,"localhost")
            .setHandler(path().addPrefixPath("/", resource(new ClassPathResourceManager(main.class.getClassLoader(), main.class.getPackage())).addWelcomeFiles("WSGSS/index.html")))
            .build()
            .start();

index.html は期待どおりに提供されますが、Websocket に接続できないようです。また、Websocket の展開方法に何か問題があると確信しています。github の例のように実行すると動作しますが、その後静的ファイルを提供する正しい方法が見つからない

JavaScriptで例外が発生します

var ws = new WebSocket("ws://localhost:8000/websockets/socket")

WebSocket connection to 'ws://localhost:8000/websockets/socket' failed: Error during WebSocket handshake: Unexpected response code: 404 

ここにも私のServerEndpointがあります

@ServerEndpoint(value = "/socket",
encoders = CommandEncoder.class,
decoders = CommandDecoder.class)
public class Socket{

    private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
    private final Logger log = Logger.getLogger(getClass().getName());

    @OnMessage
    public void onMessage(Session session,Command command){
        log.info("command of type "+ command.getAction() + " recieved");
        command.Process();
        try{
            session.getBasicRemote().sendObject(command);
            //Future<Void> future = session.getAsyncRemote().sendText(message);
        } catch (IOException | EncodeException ex){
            log.info(ex.getMessage() + ex.getCause());
        }
    }

    @OnOpen
    public void  onOpen(Session session){
        sessions.add(session);
        log.info("new session created "+ session.getId());
    }

    @OnClose
    public void onClose(Session session){
        sessions.remove(session);
    }

    @OnError void onError(Session session,Throwable thr){
        log.info(session.getId() + " error " + thr.getMessage() + thr.getCause());
    }
}
4

1 に答える 1