如何将Node.js WebSocket服务器部署到Amazon Elastic Beanstalk? [英] How to deploy a Node.js WebSocket server to Amazon Elastic Beanstalk?

查看:218
本文介绍了如何将Node.js WebSocket服务器部署到Amazon Elastic Beanstalk?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Elastic Beanstalk Web控制台,我已经推出了一个新的Web Server 1.0环境:




  • 预定义的配置:Node.js ,64位Amazon Linux 2014.09 v1.0.9

  • 环境类型:负载平衡,自动缩放



将代理服务器设置为无。



我已成功压缩&通过控制台上传我的代码:



package.json

  
name:cool,
version:0.0.0,
dependencies:{
ws:0.4.x
}
}

server.js

  var wss = new(require('ws'))。Server({port:(process.env.PORT || 3000)})
wss。 on('connection',function(ws){
console.log('connected')
ws.on('message',function(message){
console.log(message)
ws.send(message)
});
ws.on('close',function(){
console.log('disconnect')
} )
})

我还尝试包括 node_modules 目录。



运行后:

  wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com/ 

我回来了: / p>

 错误:意外的服务器响应(200)
pre>

如何找到有关该错误的更多信息?



Amazon将PORT环境变量设置为8080。所以,我也试过:

  wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com:8080 / 

但是,刚刚挂起然后返回:

 错误:连接ETIMEDOUT 

此外,当我建立一个WebSocket连接,我希望服务器允许它无限期地保持打开,不像Heroku的WebSocket实现,60秒后超时。



有没有详细的教程如何这样做?

解决方案

如果您设置了 package.json 文件正确(主要是使用 - 保存 npm install ing),那么你不应该包括node_modules目录。



检查 process.env.PORT 设置是否不变e侦听端口 - AWS EB通常将端口设置为8081。



要查找正在报告的端口,可以添加 console.log(process。 env.PORT),然后通过ssh连接到服务器并运行 tail -f var / log / nodejs / nodejs.log (这将监视node.js的日志输出)。然后再次打开您的服务器,看看ssh连接的日志输出中显示的端口。



要调查您收到的错误,请添加一个 ws.on('error',...)函数并记录你想要的。


Using the Elastic Beanstalk web console, I've launched a new Web Server 1.0 environment with:

  • Predefined configuration: Node.js, 64bit Amazon Linux 2014.09 v1.0.9
  • Environment type: Load balancing, autoscaling

and have set Proxy Server to none.

I've successfully compressed & uploaded via the console my code:

package.json

{
  "name": "cool",
  "version": "0.0.0",
  "dependencies": {
    "ws": "0.4.x"
  }
}

server.js

var wss = new (require('ws')).Server({port: (process.env.PORT || 3000)})
wss.on('connection', function(ws) {
  console.log('connected')
  ws.on('message', function(message) {
    console.log(message)
    ws.send(message)
  });
  ws.on('close', function() {
    console.log('disconnected')
  })
})

I also tried including the node_modules directory.

After running:

wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com/

I got back:

Error: unexpected server response (200)

How can I find out more about that error?

Amazon sets the PORT environment variable to 8080. So, I also tried:

wscat -c ws://default-environment-xxxxxxxxxx.elasticbeatalk.com:8080/

But, that just hung and then returned:

Error: connect ETIMEDOUT

Also, when I do establish a WebSocket connection, I want the server to allow it to stay open indefinitely, unlike Heroku's WebSocket implementation, which times out after 60 seconds.

Is there a detailed tutorial on how to do this?

解决方案

If you have set up your package.json file correctly (primarily by using --save when npm installing) then you should not have to include the node_modules directory.

Check that the process.env.PORT setting is not changing the listened port - AWS EB usually sets the port to 8081.

To find the port being reported, you can add console.log(process.env.PORT) in your code, then connect via ssh to the server and run tail -f var/log/nodejs/nodejs.log (this monitors the log output of node.js). Then hit your server again and see what port shows up in the log output on your ssh connection.

To investigate the error you are getting, add a ws.on('error',...) function and log what you want.

这篇关于如何将Node.js WebSocket服务器部署到Amazon Elastic Beanstalk?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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