2016-06-21 112 views
1

我想部署我的Node.js,Express.js,Socket.IO和基於React的應用程序到Heroku上。Heroku - Socket.IO客戶端總是連接到本地主機

但是在客戶端,socket.io-client似乎總是連接到本地主機,即使我在連接時從未指定過本地主機。

我得到這個錯誤在瀏覽器控制檯:

bundle.js:208 Mixed Content: The page at 'https://test.herokuapp.com/#/test?_k=v7l28t' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LLobEYP'. This request has been blocked; the content must be served over HTTPS. 

如果我嘗試打開它在http我得到一個連接被拒絕的錯誤:

bundle.js:208 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LLpAsLm net::ERR_CONNECTION_REFUSED 

這裏是我的服務器套接字的樣子:

var express = require('express'); 
var app = express(); 
var server = http.createServer(app); 
var io = require('socket.io')(server); 
server.listen(port); 
io.on('connection', function(socket) { 
    socket.on('news', function(message) { 
    console.log("Responding with news"); 
    io.sockets.emit('news', 'news'); 
    }); 
}); 

而這裏的客戶端代碼:

var io = require('socket.io-client'); 
var socket = io.connect(); 
socket.on('connect', function() { 
    console.log('connected to server'); 
    socket.emit('news', 'news'); 
}); 

加上的package.json:

{ 
    "name": "test-socket", 
    "version": "1.0.0", 
    "private": true, 
    "scripts": { 
    "dev-server": "nodemon ./bin/www", 
    "dev-client": "watchify -t [ envify --NOVE_ENV development ] -t [ babelify --presets [ react es2015 ] ] public/javascripts/index.js -o public/javascripts/build/bundle.js -v", 
    "dev": "concurrently \"npm run dev-server\" \"npm run dev-client\"", 
    "build": "browserify -t [ envify --NOVE_ENV production ] -t [ babelify --presets [ react es2015 ] ] -g uglifyify public/javascripts/index.js -o public/javascripts/build/bundle.js", 
    "start": "node ./bin/www" 
    }, 
    "dependencies": { 
    "body-parser": "~1.13.2", 
    "cookie-parser": "~1.3.5", 
    "debug": "~2.2.0", 
    "express": "~4.13.1", 
    "fakeredis": "^1.0.3", 
    "jade": "~1.11.0", 
    "lodash": "^4.11.1", 
    "material-ui": "^0.15.0-beta.2", 
    "mongoose": "^4.5.0", 
    "morgan": "~1.6.1", 
    "passport": "^0.3.2", 
    "passport-http": "^0.3.0", 
    "passport-local": "^1.0.0", 
    "react": "^15.0.1", 
    "react-bootstrap": "^0.28.5", 
    "react-dom": "^15.0.1", 
    "react-router": "^2.4.1", 
    "react-tap-event-plugin": "^1.0.0", 
    "redis": "^2.6.2", 
    "serve-favicon": "~2.3.0", 
    "socket.io": "^1.4.6", 
    "socket.io-client": "^1.4.6", 
    "whatwg-fetch": "^0.11.0" 
    }, 
    "devDependencies": { 
    "babel-preset-es2015": "^6.6.0", 
    "babel-preset-react": "^6.5.0", 
    "babelify": "^7.2.0", 
    "browserify": "^13.0.0", 
    "concurrently": "^2.1.0", 
    "envify": "^3.4.0", 
    "nodemon": "^1.9.2", 
    "uglifyify": "^3.0.1", 
    "watchify": "^3.7.0" 
    }, 
    "description": "", 
    "main": "app.js", 
    "author": "Harsha Bhat", 
    "license": "ISC" 
} 

請讓我知道,如果我在這裏做得不對。感謝您的幫助:)

+0

你能不能把你的'package.json'打開你的網站?我相信可能有一個腳本調用localhost服務器 –

+0

當然。添加相同。 –

+0

打開您網站的http版本'http:// test.herokuapp.com'並且它應該可以工作 –

回答

0

您的網站在https上運行,但在http上的套接字上運行。使用您的初始連接安全的URL,即代替"http://"使用"https://"

var socket = io.connect('https://test.herokuapp.com', {secure: true}); 

或者在http://test.herokuapp.com(無SSL)

+0

即使我打開http,它仍然指向localhost:3000,並且出現此錯誤:net :: ERR_CONNECTION_REFUSED –

+0

您可能已經獲得硬編碼url在某處,檢查項目中是否有任何其他io.connect調用。至少爲了測試目的而明確寫入url –

相關問題