我在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!nn");
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模拟器连接到这个套接字时,我没有得到响应。安卓工作室中的连接代码如下:
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);
}
}
...
我的代码有什么问题吗?
Nvm我发现了问题。目前,对于Openshift,websocket在8080端口上侦听,但当连接到它时,应用程序必须连接到8000端口