I'm not too sure if I'm going about this in the right way. I want to stick with my Socket.IO server, and do not want to create a separate HTTP server within node. With this, can I create a PHP client that can send data (e.g.: player buys item from online shop) to the node Socket.IO server directly?
我不太确定我是否会以正确的方式解决这个问题。我想坚持使用我的Socket.IO服务器,并且不想在节点内创建单独的HTTP服务器。有了这个,我可以创建一个PHP客户端,可以直接将数据(例如:玩家从在线商店购买商品)发送到节点Socket.IO服务器吗?
I've started with this:
我从这开始:
<?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.');
}
}
?>
The socket seems to connect just fine to the node server, but how can I start receiving data directly from the PHP client?
套接字似乎可以很好地连接到节点服务器,但是如何直接从PHP客户端开始接收数据呢?
E.g.: Say I use socket_write
to send a message to the server. How do I get that through Socket.IO?
例如:假设我使用socket_write向服务器发送消息。我如何通过Socket.IO获得它?
Hope my question makes sense!
希望我的问题有道理!
3 个解决方案
#1
4
im using http://elephant.io/ for comunication between php and socket.io, i have only problems with the time to stablish connection, 3 or 4 seconds to complete sending data.
我使用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();
#2
3
With the latest version of ElephantIO
, I've managed to send a message with:
使用最新版本的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!!';
}
#3
0
ElephantIO still not yet updated to the latest Socket.IO.
ElephantIO尚未更新到最新的Socket.IO。
Because socket.io v0.9 and v1.x is very different.
因为socket.io v0.9和v1.x非常不同。
Socket.io v1.0 here : http://socket.io/blog/introducing-socket-io-1-0/
Socket.io v1.0在这里:http://socket.io/blog/introducing-socket-io-1-0/
Updated : This link may help : https://github.com/psinetron/PHP_SocketIO_Client
更新:此链接可能有所帮助:https://github.com/psinetron/PHP_SocketIO_Client
#1
4
im using http://elephant.io/ for comunication between php and socket.io, i have only problems with the time to stablish connection, 3 or 4 seconds to complete sending data.
我使用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();
#2
3
With the latest version of ElephantIO
, I've managed to send a message with:
使用最新版本的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!!';
}
#3
0
ElephantIO still not yet updated to the latest Socket.IO.
ElephantIO尚未更新到最新的Socket.IO。
Because socket.io v0.9 and v1.x is very different.
因为socket.io v0.9和v1.x非常不同。
Socket.io v1.0 here : http://socket.io/blog/introducing-socket-io-1-0/
Socket.io v1.0在这里:http://socket.io/blog/introducing-socket-io-1-0/
Updated : This link may help : https://github.com/psinetron/PHP_SocketIO_Client
更新:此链接可能有所帮助:https://github.com/psinetron/PHP_SocketIO_Client