Skip to content

How do I listen to a UNIX domain socket in a single-process HTTP/Web socket server ? #5308

Answered by NathanFreeman
henrywood asked this question in Q&A
Discussion options

You must be logged in to vote
<?php
$server = new Swoole\Http\Server("0.0.0.0", 8501);
$server->set([
    'backlog' => 128,
]);

$unixSocket = $server->addlistener("/var/run/myserv.sock", 0, SWOOLE_UNIX_STREAM); //UnixSocket Stream
$unixSocket->set([]);
$unixSocket->on('Receive', function(Swoole\Server $server, int $fd, int $reactorId, string $data) {
    $server->send($data);
});

$server->on('Request', function(Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello, TCP World!\n");
});

$server->start();

call the $unixSocket->set([]); method to override the settings of the main server
see https://wiki.swoole.com/en/#/server/port?…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@NathanFreeman
Comment options

@henrywood
Comment options

@henrywood
Comment options

@NathanFreeman
Comment options

Answer selected by henrywood
@henrywood
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants