2015-11-13 1 views
4

Je suis nouveau en utilisant undertow, je développe une application autonome qui l'utilisera comme serveur embarqué. Je souhaite pouvoir déployer des sockets Web, des servlets et des services htmls restfull dans mon flux intégré. Jusqu'à présent, j'ai seulement fait le test avec des sockets web et des servlets, le problème que chaque fonction fonctionne correctement, mais déployez-les ensemble des sockets web pas d'accès depuis une page de test avec HTML et JavaScript, si je supprime le servlet Le test nigun de la page fonctionne sans erreur.Embedded Undertown déployer websocket et servlet en même temps, ne pas travailler

Ceci est mon code:

/* 
    * Create the appWebSocketDeploymentInfo and configure 
    */ 
    WebSocketDeploymentInfo appWebSocketDeploymentInfo = new WebSocketDeploymentInfo(); 
    appWebSocketDeploymentInfo.setBuffers(new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 17000, 17000 * 16)); 
    appWebSocketDeploymentInfo.addEndpoint(WebSocketEndpoint1.class); 
    appWebSocketDeploymentInfo.addEndpoint(WebSocketEndpoint2.class); 

    /* 
    * Create the appDeploymentInfo and configure 
    */ 
    DeploymentInfo appDeploymentInfo = Servlets.deployment() 
            .setClassLoader(Server.class.getClassLoader()) 
            .setContextPath("/myapp) 
            .setDeploymentName("app.war") 
            .setResourceManager(new FileResourceManager(new File("src/main/webapp"), 1024)) 
            .addServlets(Servlets.servlet("HomeServlet", HomeServlet.class).addMapping("/home")) 
            .setResourceManager(new ClassPathResourceManager(Server.class.getClassLoader(), Server.class.getPackage())) 
            .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, appWebSocketDeploymentInfo); 

    /* 
    * Create the deploymentManager 
    */ 
    deploymentManager = Servlets.defaultContainer().addDeployment(appDeploymentInfo); 

    /* 
    * Deploy the app 
    */ 
    deploymentManager.deploy(); 

    /* 
    * Create the path handle 
    */ 
    pathHandler = Handlers.path(Handlers.redirect("/myapp/home")).addPrefixPath("/myapp", deploymentManager.start()); 

    /* 
    * Create the server 
    */ 
    undertowServer = Undertow.builder().addHttpListener(DEFAULT_PORT, DEFAULT_IP).setHandler(pathHandler).build(); 

L'erreur javascript journal est

connexion WebSocket à 'ws: // localhost: 8080/Fermat/node-canal' a échoué: Erreur lors de poignée de main WebSocket : Réponse inattendue code: 404

+0

Pas vraiment une réponse à votre question, mais plutôt un indice: avez-vous pensé à utiliser Spring Boot pour cela? Il supporte aussi Undertow et s'occupera automatiquement de ce que vous faites manuellement. – Marged

+1

merci, mais je ne sais pas beaucoup sur le printemps et ne voulais vraiment pas l'utiliser. – Roberto

+0

J'ai trouvé cet exemple: https://github.com/fourcube/guice-undertow-websockets – dsmith

Répondre

4

Après des tests approfondis et de la recherche, j'ai obtenu la bonne façon de faire la configuration et démarrer le serveur, va également ajouter un soutien pour d'autres technologies telles que:

  • WebSocket
  • Resteasy
  • Weld

Mon code final:

UndertowJaxrsServer server = new UndertowJaxrsServer(); 
Undertow.Builder serverBuilder = Undertow.builder().addHttpListener(DEFAULT_PORT, DEFAULT_IP); 
ServletContainer servletContainer = Servlets.defaultContainer(); 

    /* 
    * Create the App WebSocketDeploymentInfo and configure 
    */ 
    WebSocketDeploymentInfo appWebSocketDeploymentInfo = new WebSocketDeploymentInfo(); 
    appWebSocketDeploymentInfo.setBuffers(new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 17000, 17000 * 16)); 
    appWebSocketDeploymentInfo.addEndpoint(WebSocketNodeChannelServerEndpoint.class); 
    appWebSocketDeploymentInfo.addEndpoint(WebSocketClientChannelServerEndpoint.class); 

    /* 
    * Create the App ResteasyDeployment and configure 
    */ 
    ResteasyDeployment deployment = new ResteasyDeployment(); 
    deployment.setApplicationClass(JaxRsActivator.class.getName()); 
    deployment.setInjectorFactoryClass("org.jboss.resteasy.cdi.CdiInjectorFactory"); 

    /* 
    * Create the App DeploymentInfo and configure 
    */ 
    DeploymentInfo appDeploymentInfo = server.undertowDeployment(deployment, APP_NAME); 
    appDeploymentInfo.setClassLoader(FermatEmbeddedNodeServer.class.getClassLoader()) 
        .setContextPath(APP_NAME) 
        .setDeploymentName(WAR_APP_NAME) 
        .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, appWebSocketDeploymentInfo) 
        .addServlets(Servlets.servlet("HomeServlet", HomeServlet.class).addMapping("/home")) 
        .addListeners(Servlets.listener(org.jboss.weld.environment.servlet.Listener.class)); 

    server.deploy(appDeploymentInfo); 
    server.start(serverBuilder); 

Gradle dépendances

compile 'io.undertow:undertow-core:1.3.6.Final' 
compile 'io.undertow:undertow-servlet:1.3.6.Final' 
compile 'io.undertow:undertow-websockets-jsr:1.3.6.Final' 

compile 'org.jboss.resteasy:resteasy-undertow:3.0.13.Final' 
compile 'org.jboss.resteasy:resteasy-cdi:3.0.13.Final' 
compile 'org.jboss.weld.servlet:weld-servlet:2.3.1.Final' 

compile 'javax:javaee-api:7.0' 
+0

J'ai essayé votre approche, mais je n'arrive pas à me lancer dans l'injection de points de terminaison websocket. Est-ce que ça marche pour toi? –

+0

En fait, j'utilise RESTful WebService côté injection de dépendance dans les points de terminaison que je n'ai pas testés. – Roberto

+0

Oui, l'injection dans les points de terminaison REST fonctionne aussi pour moi. Impossible de l'utiliser pour les points de terminaison websocket. Il semble que la spécification websocket ne spécifie pas comment l'intégration avec CDI est censée fonctionner, quels contextes sont en jeu, etc. –