简体   繁体   中英

websockets in tomcat7 spring boot app

I have a spring boot war file I'm deploying to a tomcat 7.0.78 instance. The web applicaiton starts up fine, but the web sockets aren't working. They run fine in an embedded tomcat environment, eg running ./gradlew bootRun .

The error I'm getting in my chrome console is:

angular.js:14525 Possibly unhandled rejection: Whoops! Lost connection to http://localhost:8080/websock

I tried setting the tomcat connector to use the Http11NioProtocol, but it seems to have no effect.

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
       connectionTimeout="20000"
       redirectPort="8443" />      

I also tried setting it as the ROOT.war, and the browse then indicates: WebSocket connection to 'ws://localhost:8080/websock/424/wzkek0zw/websocket' failed: Error during WebSocket handshake: Unexpected response code: 500.

Is there somethign in a tomcat config file I'm missing to properly enable websockets? My web socket configuration class looks like this:

@Configuration
@EnableWebSocketMessageBroker
@EnableWebSocket
public class WebSocketConfiguration  extends AbstractWebSocketMessageBrokerConfigurer {


    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websock").setAllowedOrigins("*").withSockJS();
    }
}

I haven't edited anything in web.xml, and have changed only the connector protocol in server.xml.

Update: Part of my problem wsa that my connection attemps had a slash in the front, eg $stomp.connect('/websock'), so I changed it to $stomp.connect('websock'). THis works fine on a windows install of tomcat, but doesn't work on an ubuntu install. I have to explicitly tell it to connect to http://localhost:8080/websock in my javascript, but then I get a follow on error trying to connect to the web socket:

webSocket connection to 'ws://localhost:8080/websock/940/qdb1xkkp/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

My issue was that I had to get the base url correct before connecitng to the socket via stomp:

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
$stomp.connect(baseUrl + '/websock', {})

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