PHP WebSocket服务器:无法连接客户端

时间:2016-04-14 11:08:55

标签: php sockets whatsapp ratchet

我有一个基于PHP的Websocket服务器,用于连接Web客户端。 WebSocket服务器本身充当WhatsApp的客户端。我希望当服务器启动时,它会调用WhatsApp并建立实时连接。下面是我的WebSocket类。由于有一个无限While循环,其他东西都没有被调用;比如连接客户端。

如何在Web Socket服务器加载时将其连接到WhatsApp服务器,然后让其他客户端连接Web Socket服务器

Chat.php

#!/usr/local/bin/php
<?php

ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__.'/vendor/autoload.php';
require_once 'vendor/whatsapp/chat-api/src/whatsprot.class.php';
require_once 'vendor/whatsapp/chat-api/src/events/MyEvents.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

use Guzzle\Http\Client;
use Guzzle\Http\EntityBody;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;



class Chat implements MessageComponentInterface {
      protected $clients;
      protected $message;
      protected $client_info = []; //To hold which agent was connected
      protected $username = 'xx';
      protected $password = 'xx=';
      protected $nickname = 'xx Number';
      protected $target = "xx";
      protected $debug = false;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
        print("..Server is coming up..\n");

        echo "[] Logging in as '$this->nickname' ($this->username)\n";
        $w = new WhatsProt($this->username, $this->nickname, $this->debug,true,__DIR__.'/wadata/');
        $events = new MyEvents($w);
        $events->setEventsToListenFor($events->activeEvents);
        $w->connect();
        $w->loginWithPassword($this->password);
        print("..Accepting WhatsApp Messages...\n");
        while (1) {
            $w->pollMessage();
        }
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        //Identity Message
        $this->message = json_encode(['STATUS'=> 'CONNECTED','CLIENT_ID' => $conn->resourceId]);
        $conn->send($this->message);
        $this->message = json_encode(['STATUS' => 'NEW', 'TYPE'=> 'TEXT','MESSAGE' => "This is by customer"]);
        //Actual WhatsApp message
        $conn->send($this->message);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        print("CLIENT SAYS:- ".$msg."\n");

        foreach ($this->clients as $client) {
            if ($from === $client) {
                $msg = 'WA Server responds:- '.$msg;
                $client->send($this->message);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    8080
);
$server->run();

0 个答案:

没有答案