适用于 socket.io 版本 3 的新答案
https://github.com/socketio/socket.io-protocol#sample-session
工作服务器示例:
直接运行服务器/usr/local/bin/node /socket_io/server/index.js
或通过'forever'运行服务器
/usr/local/bin/forever -a -l /logs/socket.io/forever-log.log -o /logs/socket.io/forever-out.log -e /media/flashmax/var/logs/socket.io/forever-err.log start /socket_io/server/index.js
index.js 文件内容(接收请求):
var io = require('/usr/local/lib/node_modules/socket.io')(8000);
//25-01-2021
// socket.io v3
//iptables -I INPUT 45 -p tcp -m state --state NEW -m tcp --dport 8000 -j ACCEPT
//iptables -nvL --line-number
io.sockets.on('connection', function (socket) {
socket.on('router', function (channel,newmessage) {
// console.log('router==========='+channel);
// console.log('router-----------'+newmessage);
socket.broadcast.to(channel).emit('new_chat',newmessage);
});
socket.on('watch_for_me', function (chanelll) {
socket.join(chanelll);
//console.log('user watch new '+chanelll);
});
socket.on('dont_watch', function (del_chanelll) {
socket.leave(del_chanelll);
//console.log('user go out' +del_chanelll);
});
});
从 PHP 发送的脚本
<?php
// php -n /socket_io/test.php
// dl('curl.so');
// dl('json.so');
$site_name='http://127.0.0.1:8000/socket.io/?EIO=4&transport=polling&t=mytest';
// create curl resource
$ch = curl_init();
//Request n°1 (open packet)
// set url
curl_setopt($ch, CURLOPT_URL, $site_name);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
$output=substr($output, 1);
$decod=json_decode($output);
//Request n°2 (namespace connection request):
$site_name2=$site_name.'&sid='.$decod->sid;
curl_setopt($ch, CURLOPT_URL, $site_name2);
curl_setopt($ch, CURLOPT_POST, true);
// 4 => Engine.IO "message" packet type
// 0 => Socket.IO "CONNECT" packet type
curl_setopt($ch, CURLOPT_POSTFIELDS, '40');
$output = curl_exec($ch);
// Request n°3 (namespace connection approval)
if ($output==='ok') {
curl_setopt($ch, CURLOPT_URL, $site_name2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
}
// Request n°4 socket.emit('hey', 'Jude') is executed on the server:
if ($output==='ok') {
curl_setopt($ch, CURLOPT_URL, $site_name2);
curl_setopt($ch, CURLOPT_POST, true);
$message_send='{\\"uuuuu\\": \\"000\\",\\"ryyyd\\":\\"999\\",\\"hyyya\\":\\"<div class=\'fro9\'>llll</div><div class=\'9ig\'>iiiii</div><div class=\'ti9g\'>iiiii</div>\\"}';
curl_setopt($ch, CURLOPT_POSTFIELDS, "42[\"router\",\"chanel@id\",\"$message_send\"]");
$output = curl_exec($ch);
print_r($output);
}
$message_send='send 2 message';
curl_setopt($ch, CURLOPT_POSTFIELDS, "42[\"router\",\"chanel@id\",\"$message_send\"]");
$output = curl_exec($ch);
$message_send='send 3 message';
curl_setopt($ch, CURLOPT_POSTFIELDS, "42[\"router\",\"chanel@id\",\"$message_send\"]");
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
奖励:apache 的设置 -> mod_proxy 访问 socket.io 可用于虚拟主机或整个服务器。
RewriteEngine on
RewriteCond %{QUERY_STRING} transport=polling [NC]
RewriteRule /(.*)$ http://127.0.0.1:8000/$1 [P,L]
ProxyRequests off
ProxyPass /socket.io/ ws://127.0.0.1:8000/socket.io/
ProxyPassReverse /socket.io/ ws://127.0.0.1:8000/socket.io/