使用ratchetphp / Pawl websocket客户端的问题

时间:2016-04-28 03:34:06

标签: slack-api phpwebsocket

我的问题是特定于在ratchetphp / Pawl上使用slack rtm。我有下面的代码连接很好但最终死了。

            <?php

            require_once "response.php";

            //first make authenticated call to rtm.start    
            use \Curl\Curl;
            $curl = new Curl();

            $rtmStartUrl = "https://slack.com/api/rtm.start?token=xx-xx-xx-xx-xx&pretty=1";
            $curl->get($rtmStartUrl);

            $wsUrl = $curl->response->url;

            $loop = React\EventLoop\Factory::create();
            $connector = new Ratchet\Client\Connector($loop);

            $connector($wsUrl)
            ->then(function(Ratchet\Client\WebSocket $conn) {
            $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
            echo "Received: {$msg}\n";
            // $conn->close();
            });

            $conn->on('close', function($code = null, $reason = null) {
            echo "Connection closed ({$code} - {$reason})\n";
            });

            $conn->send('Hello World!');
            }, function(\Exception $e) use ($loop) {
            echo "Could not connect: {$e->getMessage()}\n";
            $loop->stop();
            });

            $loop->run();

运行时,输出为:

            root@ip-172-31-45-75:/var/www/html/slack# php pawl.php 
            Received: {"type":"hello"}
            Received: {"type":"reconnect_url","url":"wss://mpmulti-qpau.slack-msgs.com/websocket/jDkgDysXfZspRj10zqdcrshHK6PhPLItYx2HEkdXy47RPCAJwKgI_NLq0bhS4uMjIT7iRtOoCDUJffcxcr7YdiqMbITUZYqnTmT39Et5a8JeuPLFfCUUzan4MCz34p0jcfAKaQW9G9HpIWrYH4CTqyICZuhgWHnzo8K7dO2zXFc="}
            Received: {}
            Connection closed (1006 - Underlying connection closed)
            root@ip-172-31-45-75:/var/www/html/slack# 

websocket是slack rtm api(https://api.slack.com/rtm)的一部分。看起来在读取空的websocket时,连接已关闭。我想我们可能会听到通过websocket发送的松弛事件(https://api.slack.com/events)以避免断开连接。

目前,由于断开连接错误,这不起作用。

1 个答案:

答案 0 :(得分:2)

Hello World!不是此API的有效消息,因此Slack在收到该消息时正在关闭连接。尝试发送有效的内容(或根本没有内容)。