简单的websocket和静态文件示例

时间:2014-07-09 03:46:18

标签: java java-ee wildfly-8 undertow

尝试设置一下用它来玩它有点,我试图服务一个静态的html文件index.html并添加我的annoted 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());
    }
}

1 个答案:

答案 0 :(得分:1)

以下解决了它

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