开发者

Which programming language should I use for TCP server for JavaScript clients?

I am asking for advise on which programming language is most acceptable for the following situation.

The program will act as the server in a TCP networking application, serving JavaScript clients that output to the browser, using a pre-written framework.

The server program will need to be 'always-on', and be capable of dealing with JSON.

My first instinct is to use PHP, because it can run the same web-server, and has pre-existing JSON and TCP functions. Is there a way to run PHP scripts on the server with开发者_JAVA百科out needing to have a browser open to 'trigger' the script execution? - the script will have to be running for hours on end without timing out.

Other languages that are considered are C#, C++, Java.

Thanks in advance.


node.js

The first example on the homepage shows how easy it is to build servers in it

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');


Anything can make a request that triggers a PHP script to be executed, it doesn't have to be a browser. For example, a cron job can simply request a particular page and it will be executed. Or it can execute it directly.


It looks like you are trying to run a php script to push data to the browser in real time. PHP is not ideal for this since it is io-blocking and will become terribly slow/crash when there are not enough available threads. This makes it very bad from a scalability standpoint. Java is ideal for this since it gives you the ability to control how threads are handled. If you are indeed looking to push data to your browser in real-time, xhr long polling is what you want. APE-server is by far the best solution I have found for this:

http://www.ape-project.org/

As a side note, you can run php scripts serverside without a browser. For a linux system, you will need php-cli to do this. To get php-cli, in terminal, type in sudo -s, hit enter, type your password, hit enter, type sudo apt-get install php-cli, hit y, then hit enter again. Then, create a file called yourfilename.run in the same directory as your php file and insert the following into the .run file:

php -f name_of_php_file.php

Allow the file to be executed in terminal (by right clicking it and selecting it), then double click it to open it in terminal. Voila, your script is running without a browser.

But once again, if you are trying to push data to your browser in real-time, php is a bad choice. Take the time to look into ape-server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜