unable to deploy nodejs socket io application on o

2019-07-11 15:30发布

问题:

I have a simple chat application based on nodejs and socket io. Works fine locally. But i am facing issues in deploying it to openshift.

I have read and implemented almost all related threads on Stack Overflow. No progress yet.

Client Side(index.html):

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect("http://www.abcd.com:8000/"); 

where 'abcd' is an attached domain name to the openshift app.

Server Side(index.js):

//Express initializes app to be a funciton handler that is to be supplied to the http server later
var express = require('express');
var app = express();
//
var server = require('http').Server(app);
var io = require('socket.io')(server);
var mongoose  = require('mongoose');

var server_port = process.env.OPENSHIFT_NODEJS_PORT || 3000
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

//server listening
server.listen(server_port, server_ip_address, function(){
  console.log('listening on *:'+server_port);
});

I have done the following already:

  • Changing the client side URL to: ws://www.abcd.com:8000/
  • replacing io.connect with io
  • switching the port to 80
  • and a lot of other things.

ERROR:

  • If i leave the declaration as: var socket = io(); THE ERROR IS WebSocket connection to 'ws://www.abcd.com/socket.io/?EIO=3&transport=websocket&sid=tyEDb4eVF0Nz0wb-AAAF' failed: Error during WebSocket handshake: Unexpected response code: 400
  • If i declare as anything like var socket = io.connect('http://www.abcd.com:8000/','forceNew':true); THE ERROR IS GET http://www.abcd.com:8000/socket.io/?EIO=3&transport=polling&t=1430395346761-6 net::ERR_CONNECTION_TIMED_OUT

回答1:

It looks to me like you aren't binding your websockets to the ip & port, after your server.listen, try this:

var socket = io.listen(server);