1
我試圖從PHP(CLI)發送GET或POST請求到Node.js/Sockets.IO應用程序,只使用基本cURL。這是我到目前爲止,我可以看到來自node.js(來自cli)的響應,但無法得到更遠。目前我只想把參數發送到控制檯。 (我可以在以後擴展)發送PHP GET請求到Sockets.IO
任何幫助將是偉大的!
(FYI:我沒看這個,Socket.io from php source,但需要更多的幫助,確切的代碼將是巨大的。)
PHP
$qry_str = "?msg_from_php=This_is_a_test123&y=20";
$ServerAddress = 'http://10.1.1.69/socket.io/1/websocket/TICWI50sbew59XRE-O';
$ServerPort = '4000';
$TimeOut = 20;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ServerAddress. $qry_str);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_PORT, $ServerPort);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $TimeOut);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
// not sure if it should be in an array
//$data = array('msg_from_php' => 'simple message!');
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = trim(curl_exec($ch));
curl_close($ch);
echo " Sent! Content: $content \r\n";
Node.js的
var express = require('express'), http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.configure('production', function(){
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('log level', 1);
io.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']);
io.set("polling duration", 30);
});
server.listen(4000); // 80,443, 843, 4000, 4001
io.sockets.on('connection', function (socket) {
socket.on('msg_from_php, function (data) {
console.log(data);
});
});
這就是正確的,即時通訊試圖做什麼。你可以給我一個鏈接,我可以得到一些關於這樣做的信息,或者甚至更好,爲它提供一些示例節點代碼?非常感謝 –