Heroku - Socket.IO客户端始终连接到Localhost

时间:2016-06-21 11:03:59

标签: javascript node.js heroku socket.io

我正在尝试将我的Node.js,Express.js,Socket.IO和基于React的应用程序部署到Heroku上。

但是在客户端,socket.io-client似乎总是连接到localhost,即使我在连接时从未指定过localhost。

我在浏览器控制台中收到此错误:

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"
}

如果我在这里做错了,请告诉我。感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

您的网站在https上运行,但在http上使用socket。使用安全网址进行初始连接,即代替"https://"使用var socket = io.connect('https://test.herokuapp.com', {secure: true});

#include<stdio.>
char sqr [3][3]
main()
{
char again;
int moveCntr, winFlag;
while (1 = = 1)
{
initliz();
sqr[1][1] = 'X';
disbord();
getmove();
makemove();
moveCntr = 3;

while(1 = = 1)
{
disbord();
getmove();
moveCntr = moveCntr + 1;

winFlag = Checkwin ('0');
if(winFlag = = 1)
{
dispbord();
printf("Congratulations!! You win.\n");
break();
}
else
{
makemove();

winFlag = checkwin ('X');
if(winFlag = = 1)
{
dispbord();
printf("I win.\n");
break;
}
}
moveCntr = moveCntr + 1;
if(moveCntr = = 9)
{
dispbord();
printf("It's a draw. Good game.\n");
}
else
{
continue;
}
}
printf("\nDo you want to play again (Y/N)?");
scanf("\n%c" ,&again);

if(again = = 'Y' || again = = 'y')
{
continue;
}
else
{
break;
}
}
return 0;
}

或者在http://test.herokuapp.com(没有ssl)

打开您的网站

答案 1 :(得分:0)

如果我用于客户端,这对我有用:

const socketURL =
  process.env.NODE_ENV === 'production'
    ? window.location.hostname
    : 'https://localhost:5000';

const socket = io.connect(socketURL, {secure: true});