将请求路由到Laravel Controller到正在运行的WebSocket服务器 [英] Route requests to Laravel Controller to running WebSocket server

查看:49
本文介绍了将请求路由到Laravel Controller到正在运行的WebSocket服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

有一个需要长期运行的任务,需要异步启动.这个任务的细节并不是很重要(基本原理是要配置多个VM和复杂的网络设置),而这是由将在不同服务器上运行的python脚本处理的.我们决定使用WebSockets在Web服务器和客户端之间来回通信,并且在那里可以进行双向通信.该网络服务器将向其他服务器发送请求,并且将在每台机器启动并运行时从python脚本接收HTTP POST通知,并在整个网络启动时最终返回HTTP POST.

There is a long-running task that needs to be launched asynchronously. The details of this task aren't really all that important (the basics are that multiple VMs are going to be provisioned and a complex network setup), and this is being handled by a python script that will be running on a different server. We decided to go with WebSockets for the communication back and forth between the web server and the client, and I have the bi-directional communication there working. The web server will be sending requests to the other server, and will receive HTTP POST notifications back from the python script when each machine is up and running, and a final HTTP POST back when the entire network is up.

所有这些工作.我们在网络服务器上使用的框架是Laravel 4,并且网络套接字服务器是使用 Ratchet 构建的,并由工匠司令部实施.我的问题是我不确定如何将HTTP POST从python脚本中继到Laravel控制器,再中继到WebSocket服务器,以便它反过来又可以将该信息中继回客户端.

All of this works. The framework we're using on the web server is Laravel 4, and the web socket server was built using Ratchet, and implemented in an artisan command. My issue is I'm not sure how to relay the HTTP POSTs to a Laravel controller from the python script to the WebSocket server so that it in turn can relay that information back to the client.

下面是Ratchet的 MessageComponentInterface :: onMessage 方法的实现,该方法使用Observer模式将任何消息事件通知给所有订阅的侦听器.

Below is the implementation of Ratchet's MessageComponentInterface::onMessage method, which is using an Observer pattern to notify any subscribed listeners of a message event.

// Class: SocketMessenger
public function onMessage(ConnectionInterface $from, $msg) {
    $respondTo = null;
    foreach($this->_clients as $client) {
        if($client->resourceId == $from->resourceId) {
            $respondTo = $client;
            break;
        }
    }

    $msg = json_decode($msg, true);

    if(!$respondTo || !$msg || !isset($msg['type'])) {
        echo 'Malformed Socket Message Received, Rejecting.';
        return;
    }

    $type = $msg['type'];

    // Notify any subscribed listeners with this message type of the 
    // incoming message. Any response received will be relayed back to 
    // the client.
    if(isset($this->_listeners[$type])) {
        foreach( $this->_listeners[$type] as $listener ) {
            $response = $listener->notify( $respondTo, $msg );
            if($response) {
                $respondTo->send( $response );
            }
        }
    }
}

WebSocket服务器是在这样的服务提供者内部构造的:

The WebSocket server is constructed within a service provider like this:

// Class: SocketServiceProvider
$app->bind( 'Ratchet\MessageComponentInterface', function( $app, $params ) {
    $port = $params && isset($params[0]) ?
        $params[0] : self::DEFAULT_PORT
    ;

    $messenger = new SocketMessenger;

    // Setup the Ratchet Server.
    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                 $messenger
            )
        ),
        $port
    );

    // Subscribe socket listeners to the socket messenger
    $messenger->subscribe( new TopologyMessageListener, TopologyMessageListener::TYPE );

    return $server;
} );

有什么方法可以在遇到指定控制器的方法时将请求发送到Laravel控制器并通知正在运行的WebSocket服务器?

Is there any way take requests to a Laravel controller and notify the running WebSocket server whenever a given controller's method is hit?

推荐答案

这里的答案似乎是我需要在PHP中创建一个WebSocket客户端,该客户端将消息发送到WebSocket服务器,这与用JavaScript编写的客户端相同将.对于在同一问题上苦苦挣扎的任何感兴趣的观众,我都能找到一些用PHP编写的WebSocket客户端:

The answer here appears to be that I need to create a WebSocket client in PHP that will send messages to the WebSocket server, the same as a client written in JavaScript would. For any interested viewers struggling with the same issue, I was able to find a few WebSocket Clients written in PHP:

  1. https://github.com/gabrielbull/php-websocket-client
  2. https://github.com/Devristo/phpws
  3. https://github.com/symbiose/php-websocket-client (<-这是我最想去的那个)
  1. https://github.com/gabrielbull/php-websocket-client
  2. https://github.com/Devristo/phpws
  3. https://github.com/symbiose/php-websocket-client (<-- This is the one I wound up going with)

如果有人想出其他想法/更好的客户端库来使用,我将暂时搁置这个问题.

I'll be leaving this question open for awhile in case anyone comes up with a different idea / better client library to use.

这篇关于将请求路由到Laravel Controller到正在运行的WebSocket服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆