side node.jsに沿ってSocket.ioを使用することをお勧めします。 libをインストールし、http://socket.io/からダウンロードしてください。あなたはそれをあなたのApacheサーバのそばに問題なく走らせることができます。
最初のノードサーバを作成します、
var http = require('http')
, url = require('url')
, fs = require('fs')
, io = require('../')//path to your socket.io lib
, sys = require(process.binding('natives').util ? 'util' : 'sys')
, server;
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
}),
server.listen(8084);//This could be almost any port number
セカンドを使用して、コマンドラインからサーバーを実行します。
node /path/to/your/server.js
第三に、クライアント側のJSを使用してソケットに接続します。
var socket = new io.Socket(null, {port: 8084, rememberTransport: false});
socket.connect();
また、socket.io libクライアント側も含める必要があります。
使用してノード・サーバにクライアント側からのデータを送信する:
socket.send({data:data});
あなたserver.jsも要求を処理する機能を持っている必要があります。
io.on('connection', function(client){
//action when client connets
client.on('message', function(message){
//action when client sends msg
});
client.on('disconnect', function(){
//action when client disconnects
});
});
をからデータを送信するには、主に二つの方法があります。クライアントへのサーバ:
client.send({ data: data});//sends it back to the client making the request
および
client.broadcast({ data: data});//sends it too every client connected to the server
こんにちは、このすばらしい答えに感謝します。私はまだApacheの部分への接続について少し混乱しています。 Apacheの部分をより明確にするために、あなたの答えにいくつかのコメント/コードを追加してもよろしいですか? –
同じ発信元ポリシーはどうですか?あなたはソケットioを使用して別のポートに電話をかけることはできませんか? – Zaptree
助けてくれてありがとう助けてくれました –