我不太确定我是否以正确的方式来做这件事。我想继续使用我的Socket.IO服务器,而不想在节点中创建单独的HTTP服务器。有了这个,我可以创建一个可以直接发送数据(例如:玩家从网上商店购买商品)到节点Socket.IO服务器的客户端吗?
我从这个开始:
<?php
class communicator {
public function connect($address, $port){
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($socket){
try {
socket_connect($socket, $address, $port);
} catch(Exception $e){
throw new Exception($e->getMessage());
}
}else{
throw new Exception('Could not create socket.');
}
}
?>
套接字似乎可以很好地连接到节点服务器,但是如何开始直接从PHP客户机接收数据呢?
例如:假设我使用socket_write
向服务器发送消息。我如何通过Socket.IO获得它?
希望我的问题有意义!
发布于 2014-03-04 06:50:10
我正在使用http://elephant.io/进行php和socket.io之间的通信,只有建立连接的时间有问题,需要3到4秒才能完成数据发送。
<?php
require( __DIR__ . '/ElephantIO/Client.php');
use ElephantIO\Client as ElephantIOClient;
$elephant = new ElephantIOClient('http://localhost:8080', 'socket.io', 1, false, true, true);
$elephant->init();
$elephant->emit('message', 'foo');
$elephant->close();
发布于 2015-10-11 08:05:25
使用最新版本的ElephantIO
,我成功地发送了一条消息:
include_once("vendor/autoload.php");
use ElephantIO\Exception\ServerConnectionFailureException;
$client = new \ElephantIO\Client(new \ElephantIO\Engine\SocketIO\Version1X('http://localhost:9000'));
try
{
$client->initialize();
$client->emit('message', ['title' => 'test']);
$client->close();
}
catch (ServerConnectionFailureException $e)
{
echo 'Server Connection Failure!!';
}
发布于 2021-01-26 07:15:46
适用于socket.io版本3的全新答案
https://github.com/socketio/socket.io-protocol#sample-session
正常工作的服务器示例:
运行服务器直接/usr/local/bin/node /socket_io/server/index.js
或者通过‘永远’运行服务器
/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/
https://stackoverflow.com/questions/21825732
复制相似问题