Websocket工作在localhost但不是Heroku

时间:2017-04-02 16:41:48

标签: angular heroku websocket spring-websocket sockjs

我有一个使用websockets的应用程序(STOMP over SockJs),后端是Spring。应用程序在Tomcat上的localhost上工作正常(websockets)但是当我部署到Heroku或AWS Web Sockets停止工作时。 我在Angular2中的websocket配置

        let sockjs = new SockJS('/rest/add?jwt=' + this.authService.getToken(), {"devel":true,"debug":true}, {"transports":["websocket"]});

我也试过

{"disabled_transports":["websocket"]}

但两者都失败了。 的web.xml

 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>default</param-value>
</context-param>

<servlet>
    <servlet-name>ws</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/ws-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>ws</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/rest/*</url-pattern>

</filter-mapping>
</web-app>

春天的websocket

    <websocket:message-broker
    application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/add">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic, /queue" />
</websocket:message-broker>

我在Heroku上的应用程序的URL,以便您可以在Web浏览器的控制台中自行检查。 Link 请查看更新的日志,这件事让我担心 No TransportHandler

CJyb2xlcyI6IkNVU1RPTUVSIn0.wFqNOduN-lD1-9GIRnG1X1aLJZTxtz9c6vmO7jmPPiE2017-04-06T16:23:32.439917+00:00 app[web.1]: 2017-04-06 16:2332 WARN  DefaultSockJsService:239 - No TransportHandler for http://myapp-ws.herokuapp.com/rest/add/097/eyocvxic/websocket?jwt=eyJhbGciOiJI

1 个答案:

答案 0 :(得分:9)

好的,所以最后我让它运转了。原来,heroku上的Tomcat没有标准的库集。 首先我在webapp-runner https://devcenter.heroku.com/articles/java-webapp-runner上本地部署,然后我看到一个奇怪的错误。我测试了几个库,最后在添加

之后
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <version>8.5.11</version>
</dependency>

<build>
...
<plugins>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals><goal>copy</goal></goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>com.github.jsimone</groupId>
                            <artifactId>webapp-runner</artifactId>
                            <version>8.5.11.3</version>
                            <destFileName>webapp-runner.jar</destFileName>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

我让它在本地工作。 然后我添加了PROCFILE以及以下内容

web:    java $JAVA_OPTS -jar myapp-ws/target/dependency/webapp-runner.jar --port $PORT myapp-ws/target/*.war

并致力于GITHUB(我有与Heroku集成的git),从Heroku内部触发构建并且它可以工作。