EC2与socket.io [英] EC2 with socket.io

查看:368
本文介绍了EC2与socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了​​一个自动气象站微比如我的节点应用。我用 socket.io 为好。我收到以下错误:

  GET的http://本地主机:3000 / socket.io / 1 / T = 1393065240268网:: ERR_CONNECTION_REFUSED
 

在此刻的控制台应建立套接字连接时。除了这个节点应用程序的工作原理。我怀疑 GET 不应该对本地主机,但对服务器的地址。

请注意,它提供服务器端节点的日志 socket.io

 调试 - 提供静态内容/socket.io.js
 

下面是我的服务器的安全组的一个画面:

Socket.io设置:

  ENV = process.env.NODE_ENV || 发展,
packageJson =需要('../的package.json),
HTTP =要求(HTTP),
EX preSS =需要('EX preSS),
RedisStore =需要('连接,Redis的)(前preSS),
SessionSockets =需要('session.socket.io),
PATH =要求('路径'),
设置=需要('./设置),
揭露=需要('EX preSS-揭露)
//配置服务器的IO和session.socket.io
tmpApp = EX preSS()
tmpServer = http.createServer(tmpApp)
IO =需要('socket.io)。听(tmpServer)
appCookieParser = EX press.cookieParser(settings.cookie.secret)
AP predisStore =新RedisStore()
sessionIO =新SessionSockets(IO,美联社predisStore,appCookieParser)

global.App = {
    应用程序:tmpApp,
    服务器:tmpServer,
    港口:process.env.PORT || 3000,
    sessionIO:sessionIO,
    IO:IO,
    启动:函数(){
        VAR的setUp = this.util(设置),
            的SocketHandler =需要('./的SocketHandler),
            自=本

            setUp.initialize(功能(ERR,waitingGames){
                如果(ERR){
                    的console.log('在初始化应用程序错误)
                    process.exit(0)
                }
                如果(!self.started){
                    self.started =真
                    self.server.listen(self.port)

                    的SocketHandler()

                    的console.log(运行应用程序版本+ App.version +端口+ App.port +中的+ App.env +模式)
                }
            })

    },
 ...
 }
 

更新

当我改变了我的口 80 我得到一个不同的错误:

  XMLHtt prequest无法加载的http://localhost/socket.io/1/ T = 1393067003774。没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地http://ec2-54-214-136-70.us-west-2.compute.amazonaws.com,因此不允许访问。
 

解决方案

我发现这个问题。这是在客户端。我被连接到本地主机。这是一个愚蠢的错误,但在开发过程中不注重这些细节,它似乎自然的 socket.io 应连接到root从您服务您的内容。

由于我使用EC2和每次重新启动后,我得到不同的DNS地址我已经发送到我初始化 socket.io 正确的页面 req.headers.host (使用 EX preSS-暴露)。

I have set up an aws micro instance for my node application. I use socket.io as well. I am getting the following error:

GET http://localhost:3000/socket.io/1/?t=1393065240268 net::ERR_CONNECTION_REFUSED

in the console at the moment when the socket connection should be created. Apart from this the node app works. I suspect that the GET should not be towards localhost but towards the address of the server.

Note that on the server side node logs that it served socket.io:

debug - served static content /socket.io.js

Here is a picture of the Security Group of my server:

.

Socket.io setup:

env = process.env.NODE_ENV || 'development',
packageJson = require('../package.json'),
http = require('http'),
express = require('express'),
RedisStore = require('connect-redis')(express),
SessionSockets = require('session.socket.io'),
path = require('path'),
settings = require('./settings'),
expose = require('express-expose')
//Configure server for io and session.socket.io
tmpApp = express(),
tmpServer = http.createServer(tmpApp),
io = require('socket.io').listen(tmpServer),
appCookieParser = express.cookieParser(settings.cookie.secret),
appRedisStore = new RedisStore(),
sessionIO = new SessionSockets(io, appRedisStore, appCookieParser)

global.App = {
    app: tmpApp,
    server: tmpServer,
    port: process.env.PORT || 3000,
    sessionIO: sessionIO,
    io: io,
    start: function() {
        var setUp = this.util('setUp'),
            socketHandler = require('./socketHandler'),
            self = this

            setUp.initialize(function(err, waitingGames) {
                if (err) {
                    console.log('error at initializing the application')
                    process.exit(0)
                }
                if (!self.started) {
                    self.started = true
                    self.server.listen(self.port)

                    socketHandler()

                    console.log("Running App Version " + App.version + " on port " + App.port + " in " + App.env + " mode")
                }
            })

    },
 ...
 }

UPDATE

When I changed my port to 80 I get a different error:

 XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1393067003774. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ec2-54-214-136-70.us-west-2.compute.amazonaws.com' is therefore not allowed access. 

解决方案

I found the problem. It was on the client side. I was connecting to localhost. It's a stupid error, but during development you don't pay attention to these details and it seemed natural that socket.io should connect to the root from where you serve your content.

Since I'm using EC2 and after each restart I get different DNS address I've sent to the page where I'm initializing the socket.io the correct the req.headers.host (using express-expose).

这篇关于EC2与socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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