Nodejs:WebSocket似乎不接受连接

时间:2016-01-13 05:55:16

标签: javascript android node.js websocket openshift

我在Openshift上托管一个简单的node.js服务器,但它似乎不接受连接,即使它正在侦听端口。我的js文件的代码如下:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

var players = [];

var WebSocketServer = require('ws').Server
var http = require('http');

console.log("ok");

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(200, {'Content-Type': 'text/plain'});
      response.write("Welcome to Node.js on OpenShift!\n\n");
      response.write(ipaddress);
      response.write(port);
      response.end("Bye");
});

server.listen( port, ipaddress, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

var wss = new WebSocketServer({
    server: server,
    autoAcceptConnections: true
});
wss.on('connection', function(ws) {
  console.log("New connection");
  ws.on('message', function(message) {
    ws.send("Received: " + message);
  });
  ws.send('Welcome!');
});

console.log("Listening to " + ipaddress + ":" + port + "...");

function player(id, x, y){
    this.id = id;
    this.x = x;
    this.y = y;
}

但是,当我尝试从Android模拟器连接到此套接字时,我得不到响应。 android studio中连接的代码如下:

public void connectSocket(){
        try {
            socket = IO.socket("http://testapp-weihao.rhcloud.com:8080");
            socket.connect();
        } catch(Exception e){
            System.out.println(e);
        }
    }
    public void configSocketEvents(){
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Gdx.app.log("SocketIO", "Connected");
                player = new Starship(playerShip);
            }
        }
...

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

Nvm我发现了这个问题。显然,对于Openshift,websocket侦听端口8080但是当连接到它时,应用程序必须连接到端口8000