我试着用rachet和pawl制作websocket服务器。我读了ZeroMQ http://socketo.me/docs/push的博士棘轮,但是不能用https://github.com/ratchetphp/Pawl来运行它
I创建客户端:
<script>
var ws = new WebSocket('ws://site.ll:8008/?user=tester01');
ws.onmessage = function(evt) { console.log(evt.data); };
</script>创建工作人员:
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Pusher.php';
$loop = React\EventLoop\Factory::create();
$pusher = new Pusher;
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8008', $loop);
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();Pusher类使用类似于教程messages中的
使用post.php通过ws:发送消息
$localsocket = 'tcp://127.0.0.1:1235';
$user = 'tester01';
$message = 'test';
$instance = stream_socket_client($localsocket);
fwrite($instance, json_encode(['user' => $user, 'message' => $message]) . "\n");,但仍然无法理解如何使用But创建tcp服务器,比如:
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:1235');
$pull->on('message', array($pusher, 'onBlogEntry'));发布于 2020-07-06 21:36:54
如果你不想使用ZeroMQ,你可以使用相同的反应机制,棘轮使用。
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Pusher.php';
use React\EventLoop\Factory;
use React\Socket\ConnectionInterface;
use React\Socket\Server;
$loop = Factory::create();
$pusher = new Pusher;
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop);
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
// Set up an incoming TCP socket to receive the messages to push
$inSocket = new Server('127.0.0.1:5555', $loop);
$inSocket->on('connection', function (ConnectionInterface $conn) use ($pusher) {
// New data arrives at the socket
$conn->on('data', function ($data) use ($conn, $pusher) {
// Push the new blog entry update
$pusher->onBlogEntry($data);
// Close the incoming connection when the message is consumed
$conn->close();
});
});
$loop->run();发布于 2018-08-22 10:46:13
处理Pawl并在github上加载:https://github.com/Shkarbatov/WebSocketPHPRatchet
https://stackoverflow.com/questions/51894681
复制相似问题