-1

I'm running ReactPHP in a Docker environment to listen for WebSocket messages. I use the port mapping feature like so:

docker run \
    --detach \
    -p 10002:8081 \
    missive-controller

Thus, the external port 10002 maps to 8081 inside the container.

Inside my implementation of MessageComponentInterface, I have this event handler:

public function onMessage(ConnectionInterface $from, $msg)
{
    echo sprintf(
        "Connection %d sent message \"%s\" to WS server\n",
        $from->resourceId,
        $msg
    );

    /* @var $request \Guzzle\Http\Message\EntityEnclosingRequest */
    $request = $from->WebSocket->request;

    // ... do stuff
}

Now, I am listening to a couple of ports in ReactPHP, in order to differentiate between internet WebSocket requests, and private messages from other containers on the private Docker network. So, to detect the port, I do this (using the Guzzle object set up above):

$request->getPort();

However, that gets me 10002 (the internet port) rather than the internal container-side port (8081). I have a mapping device to look up the association for now, but can I obtain the container port directly?

I am running Ratchet 0.36.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Silly to ask but did you try a var_dump on the request object to see if the port is to seen somewhere? Technically it is getting the right port so need to evaluate options – Tarun Lalwani Aug 05 '17 at 17:05
  • Also post the code where you set both the listeners for two ports – Tarun Lalwani Aug 05 '17 at 17:17
  • @TarunLalwani: good idea about dumping variables, I will try that - thanks. I am not sure how the code for listening to multiple ports will help in this regard, but if the var dump does not yield anything, I will add that to the post. – halfer Aug 05 '17 at 20:56

1 Answers1

0

My present answer to this is that it is not possible, at least with the version of Ratchet I am using. I have scanned $from, and this only seems to know about the port from an external perspective. The only references I can find in a print_r($from) to the internal port are ones that I have set in a manual mapping array, which does not help.

I idly pondered whether this information might be sitting in the Ratchet\Server\IoServer server instance, so I injected this into my listener before starting the loop, and examined it with print_r() again. Same result, I'm afraid; nothing pops out as looking useful.

Contrary to the original post, my port mappings are now set up in a docker-compose.yml file, so my solution for now will be to add these mappings in environment vars in the same file, so it is harder for them to come out of sync. I don't wish to hardwire these, as I am using different port mappings depending on environment.

It is possible that version 0.4 will support this, so I will revisit when I upgrade.

halfer
  • 19,824
  • 17
  • 99
  • 186