我在本地主機上運行時有一個工作的node.js服務器,我可以從我的客戶端(用java編寫)發送和接收數據。但是,當我將server.js移到Openshift的VPS應用程序(免費)時,客戶端不能再連接到server.js。將Node.js TCP服務器從本地主機移動到VPS(Openshift)
我已經按照說明上傳了文件,我通過終端到服務器打開SSH連接,導航到repo文件夾(上傳的文件在哪裏),運行「node server.js」(它似乎在線,沒有誤差和上。(「在線」,..)事件激活。
當我再我的電腦上運行的客戶端是無法連接到server.js。
server.js :
var os = require('os');
var HOST = process.env.OPENSHIFT_NODEJS_IP;;
var PORT = process.env.PORT || 80;
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var net = require('net');
console.log("Hostname: " + HOST + "\nPort: " + PORT);
if (cluster.isMaster) {
cluster.SCHED_RR;
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
Object.keys(cluster.workers).forEach(function(id){
console.log("I am runnong with ID: " + cluster.workers[id].process.pid);
});
console.log('\n');
cluster.on('online', function(worker) {
console.log('Worker: ' + worker.process.pid + " listning on port " + PORT);
});
cluster.on('exit', function(worker, code, signal){
console.log("Worker " + worker.process.pid + " died")
});
} else {
// Load the TCP Library
net = require('net');
// Keep track of the chat clients
var clients = [];
// Start a TCP Server
var server = net.createServer(function (socket) {
console.log('\n');
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n\n");
broadcast(socket.name + " joined the chat\n", socket);
// Handle incoming messages from clients.
socket.on('data', function (data) {
//var ls = net.connect(5001, 'localhost');
var string = data + "";
//console.log(string);
var message = string.split("|");
broadcast(" Passing data from "+message[2]+" to "+message[1] + "\n " + message[3], socket);
//ls.write(string);
//ls.end();
});
socket.on('connect', function(){
broadcast("\n New connection opened.\n");
});
// Remove the client from the list when it leaves
socket.on('end', function() {
clients.splice(clients.indexOf(socket), 1);
broadcast("\n" + socket.name + " left the chat.\n");
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
})
server.listen(PORT, HOST);
server.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying...');
setTimeout(function() {
server.close();
server.listen(PORT, HOST);
}, 1000);
}
else if (e.code == 'ECONNREFUSED') {
console.log('Connection refused');
}
});
}
Client.java(相關部分)
public static void main(String args[]) {
String s = getPackage();
try {
System.out.print("\n");
/*InetAddress addr;
Socket sock = new Socket("ex-std-node272.prod.rhcloud.com", 80);
addr = sock.getInetAddress();
System.out.println("Connected to " + addr);*/
Socket skt = new Socket("127.10.100.1", 80);
BufferedReader inStream = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
DataOutputStream outStream = new DataOutputStream(skt.getOutputStream());
//System.out.print(" Received string: '");
//while (!inStream.ready()) {}
//System.out.print(inStream.readLine()); // Read one line and output it
System.out.print("'\n\n");
// Send first message
//outStream.writeByte(1);
outStream.writeUTF(s);
//outStream.flush(); // Send off the data
//outStream.write(b,0,b.length);
outStream.flush(); // Send off the data
}
catch(Exception e) {
System.out.print(" Error: " + e);
}
}
當我運行server.js(在VPS)它給我的主機名:127.10.100.1和端口:80,然後我粘貼到客戶端。
那麼,我需要做的代碼,我必須使它連接?
如果(且僅當)我從刪除主機監聽呼叫和server.js更改端口爲8080,它開始聽8080,但我仍然無法從客戶端連接(將端口更改爲8080/8000(保持ip))更多想法? – Dazing 2015-03-02 21:06:22
您使用的是哪種盒式磁帶?您不應該使用ssh服務器來運行您的應用程序。作爲模板:https://github.com/openshift-quickstart/openshift-nodejs-http-and-websocket-example你應該怎麼做 – 2015-03-02 21:15:29
我已經能夠使用示例中的客戶端和服務器,但是我的java客戶只是不想,如果你有任何想法如何解決這個隨意告訴moi,但感謝您的幫助! – Dazing 2015-03-03 17:13:55