1

I am trying to configure web sockets with spring using a Wildfly 10 server. As per this tutorial, I have the following files:

This is the web socket class:

package com.myapp.spring.web.controller;

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

import org.springframework.web.socket.server.standard.SpringConfigurator;


@ServerEndpoint(value="/serverendpoint", configurator = SpringConfigurator.class)

/**
 * This class creates web sockets, opens, and maintains connection with the client
 */
public class serverendpoint {


    @OnOpen
    public void handleOpen () {
        System.out.println("JAVA: Client is now connected...");
    }

    @OnMessage
    public String handleMessage (String message) {

        if (message.equals("ping"))
            return "pong";
        else if (message.equals("close")) {
            handleClose();
            return null;
        }
        System.out.println("JAVA: Received from client: "+ message);
        if (message.contains("//")) {
            MyClass mc = new MyClass(message);
            return mc.someMethod();
        } else {
            System.out.println("Message From Web Socket Not Understood");
            return null;
        }
    }

    @OnClose
    public void handleClose() {
        System.out.println("JAVA: Client is now disconnected...");
    }

    @OnError
    public void handleError (Throwable t) {
        t.printStackTrace();
    }
}

This is the web socket config file:

package com.myapp.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import com.myapp.spring.web.controller.serverendpoint;

@Configuration
public class EndpointConfig {

    @Bean
    public serverendpoint serverendpoint() {
        return new serverendpoint();
    }

    @Bean
    public ServerEndpointExporter endpointExporter() {
        return new ServerEndpointExporter();
    }

}

This is my pom.xml:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>

According to the tutorial, this is all I have to do. But I get the following errors:

Failed to start service jboss.undertow.deployment.default-server.default-host./ROOT: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./ROOT: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:231)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    ... 6 more
Caused by: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
    at io.undertow.websockets.jsr.Bootstrap$WebSocketListener.contextInitialized(Bootstrap.java:104)
    at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
    ... 8 more

What is the fix to this problem? In addition, are there any other config files I need to add in order for my web socket to be mapped correctly at the endpoint /serverendpoint as I did in my serverendpoint() class (I am asking this because I am a bit unsure if I only need one config file or not. It doesn't seem right. I looked around and others have included other files with, for instance, the @EnableWebSocket, but the tutorial says that I only need these two files.)?

Thank you so much!

Community
  • 1
  • 1
user5139637
  • 775
  • 3
  • 10
  • 29

1 Answers1

1

Please go through https://github.com/spring-projects/spring-boot/issues/6166 and see if this solves your issue. There is a similar issue reported in SO at Spring Boot Websockets in Wildfly. Hope this helps.

Community
  • 1
  • 1
abaghel
  • 14,783
  • 2
  • 50
  • 66
  • I will look into that, thank you so much. But aside from the config file, do I need any other files for my web socket to work on spring? – user5139637 Aug 19 '16 at 02:39
  • The tutorial you are following has a sample project available at https://github.com/rstoyanchev/spring-websocket-test. Please see if that can help you. – abaghel Aug 19 '16 at 02:51
  • Now, I get a new error: Caused by: javax.websocket.DeploymentException: UT003023: Multiple endpoints with the same logical mapping PathTemplate{template=false, base='/serverendpoint', parts=[]} and PathTemplate{template=false, base='/serverendpoint', parts=[]} – user5139637 Aug 19 '16 at 03:17
  • Did you change something? Try maven clean package, un-deploy old build from jboss and restart the server. – abaghel Aug 19 '16 at 03:25
  • No, though I added an undertow dependency to my pom.xml – user5139637 Aug 19 '16 at 03:26
  • And you added that dependency with provided? – abaghel Aug 19 '16 at 03:34
  • Yes I added the dependency with that scope – user5139637 Aug 19 '16 at 03:34
  • I still get the same error after I cleaned my package (locally), restarted my (non-local) server, and deployed my code to it. So in my EditConfig class, when I call the server endpoint method with the bean annotation, does that create a second /serverendpoint that in my server endpoint class I am defining with @ServerEndpoint(value="/serverendpoint", configurator = SpringConfigurator.class)? – user5139637 Aug 19 '16 at 03:42
  • Use "@ServerEndpoint" with SpringConfigurator if you want a new instance per WebSocket session. If you want to use a single instance then use "@Bean" in Config. Not the both at the same time. – abaghel Aug 19 '16 at 04:24