amazon-web-services - AWS ElasticBeanstalk Tomcat 和 Spring 中的 Websocket 无法正常工作

标签 amazon-web-services tomcat spring-websocket amazon-elastic-beanstalk

我在 AWS EB 中使用 Tomcat。我开发了一个应用程序,使用 Spring boot 技术堆栈 Spring 4 和 Java 8。我的应用程序使用 Spring WebSocket 向用户发送通知。以下是 javascript 条目

<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js" ></script>

// Create and init the SockJS object
var socket = new SockJS('/partnervestcrm/message');
var stompClient = Stomp.over(socket);
// Subscribe the '/notify' channell
stompClient.connect({}, function(frame) {
  stompClient.subscribe('/user/queue/notify', function(notification) {
    // Call the notify function when receive a notification
    //notify(JSON.parse(notification.body).message);
    notify(JSON.parse(notification.body));
  });


}, function(error) {
    alert("STOMP error " + error);
});

当部署到 AWS EB 时,这在我的本地运行良好,但出现以下错误

WebSocket connection to 'ws://xxxxx/notification/298/plu4ia32/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
WrappedWebSocket @ VM152:164
e.exports @ websocket.js:6
r @ websocket.js:32
r._connect @ main.js:219
r._receiveInfo @ main.js:193
n @ emitter.js:30
r.emit @ emitter.js:50
(anonymous) @ info-receiver.js:67
n @ emitter.js:30
r.emit @ emitter.js:50
(anonymous) @ info-ajax.js:37
n @ emitter.js:30
r.emit @ emitter.js:50
xhr.onreadystatechange @ abstract-xhr.js:124

你能告诉我如何解决这个问题吗?

最佳答案

为安全组添加入站规则解决了这个问题。请在 ec2 dashboard > NETWORK & SECURITY(在左 Pane 中)> security groups > inbound 添​​加规则

enter image description here

关于amazon-web-services - AWS ElasticBeanstalk Tomcat 和 Spring 中的 Websocket 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640648/

相关文章:

tomcat - 如何在 Tomcat 7 - Amazon ec2 ubuntu 实例中安装 ssl

ssl - 尽管使用 SSL,任何人都可以连接到我的服务

java - 如何摆脱 tomcat 默认 session ID?

java - Spring 4 Web 套接字 - 我必须有 stomp 代理吗?

spring - 在 Spring Websockets 上构建自定义 API

amazon-web-services - AWS Elasticsearch 服务 : Disable index auto creation (auto_create_index)

amazon-web-services - 对数据库使用RDS还是容器(ECS)?的优点和缺点

amazon-web-services - 使用 DynamoDB 和 docClient,是否可以在使用 transactWrite 时获取返回值?

amazon-s3 - 如何检测 Amazon S3 中的变化?

java - 无法使用 Spring Websocket 将客户端连接到服务器