Websocket在本地主机上运行,​​但在Heroku上不运行 [英] Websocket working on localhost but not Heroku

查看:240
本文介绍了Websocket在本地主机上运行,​​但在Heroku上不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that uses websockets (STOMP over SockJs), with Spring at the backend. Application works fine (websockets) on localhost on Tomcat but when I deploy to Heroku or AWS Web Sockets stop working. My websocket configuration in Angular2

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

我也尝试过

{"disabled_transports":["websocket"]}

,但是都失败了. web.xml

but both are failing. 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: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浏览器的控制台中自行检查. 链接 请查看更新的日志,这让我担心没有TransportHandler

URL to my application on Heroku so you can check by yourself in console of your web browser. Link Please see updated logs, this piece makes me worried 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

推荐答案

好的,所以终于使它生效了.原来,heroku上的Tomcat没有标准的库集. 首先,我在本地安装了webapp-runner https://devcenter.heroku.com/articles/java-webapp-runner 之后,II看到了一个奇怪的错误.我测试了一些库,最后添加

ok, so finally I made it working. Turned out that Tomcat on heroku does not have the standard set of libraries. First I deployed locally on webapp-runner https://devcenter.heroku.com/articles/java-webapp-runner after which I I saw a strange error. I tested with a few libraries and finally after after adding

<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

I got it working locally. Then I added PROCFILE with following content

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

并致力于GITHUB(我已经将git与Heroku集成在一起),从Heroku内部触发了构建,并且有效.

and committed to GITHUB (I have git integrated with Heroku), triggered build from within Heroku and it works.

这篇关于Websocket在本地主机上运行,​​但在Heroku上不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆