Laravel SocketIO没有执行回调函数

时间:2019-01-08 14:59:41

标签: angular laravel-5 socket.io angular7 ratchet

我创建了一个Laravel项目,该项目具有使用Laravel-Ratchet的SocketIO服务器。

<?php
namespace App\Http\Listeners;

use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\Server\IoServer;

class Chat extends IoServer implements MessageComponentInterface {
   protected $clients;

   public function __construct() {
       $this->clients = new \SplObjectStorage;
   }

   /**
    * When a new connection is opened it will be passed to this method
    * @param  ConnectionInterface $conn The socket/connection that just connected to your application
    * @throws \Exception
   */
   function onOpen(ConnectionInterface $conn) {
       echo "someone connected\n";

       $this->clients->attach($conn);

       $conn->send('Welcome!');
   }

   /**
    * This is called before or after a socket is closed (depends on how it's closed).  SendMessage to $conn will not result in an error if it has already been closed.
    * @param  ConnectionInterface $conn The socket/connection that is closing/closed
    * @throws \Exception
    */
   function onClose(ConnectionInterface $conn) {
       echo "someone has disconnected\n";

       $this->clients->detach($conn);
   }

   /**
    * If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
    * the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
    * @param  ConnectionInterface $conn
    * @param  \Exception $e
    * @throws \Exception
    */
   function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
   }

   /**
    * Triggered when a client sends data through the socket
    * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
    * @param  string $msg The message received
    * @throws \Exception
    */
    function onMessage(ConnectionInterface $from, $msg) {
       echo "Someone sent a message: {$msg}\n";

       foreach ($this->clients as $client) {
           if ($from !== $client) {
               $client->send('Hello');
           }
       }
    }
}

php artisan ratchet:serve --driver=IoServer -z --class App\\Http\\Listeners\\Chat一起运行的

在前端,我正在使用Angular 7,并尝试了多个套接字。以下是尝试使用socket.io-client

chat.socket.ts:

public ngOnInit():void {
  this.socket = io(
    'http://localhost:8080',
    {
      path: '/'
    }
  );

  this.connection = this.getMessages().subscribe(
    (message:any) => {
      console.log(message);

      this.messages.push(message);
    }
  );
}

public ngOnDestroy(): void {
  this.connection.unsubscribe();
}

getMessages() {
  return new Observable(
    observer => {
      this.socket.on(
        'message',
        (data) => {
          console.log(data);

          observer.next(data);
        }
      );

      return () => {
        this.socket.disconnect();
      };
    }
  );
}

我也曾尝试使用ngx-socket-io遇到相同的问题。

chat.socket.ts:

@Injectable()
export class ChatSocket extends Socket {
   constructor() {
     super(
      {
        url: 'http://localhost:8080',
        options: {
          path: '/'
        }
      }
   );
  }
}

chat.page.ts:

public ngOnInit():void {
  this.socket.emit('init');

  this.socket.emit(
    'message',
    'Hello, World',
    function() {
        console.log('here');
     }
   );
 }

我的网络标签显示它触发了响应,但立即收到net::ERR_INVALID_RESPONSE,但没有响应。

Network Request

这是我的套接字头:

Socket Headers

使用socket-io.client时,经过一连串的失败后,我得到了这个信息:

enter image description here

发生这种情况时,终端显示我正在接收连接并断开了从应用发送的事件。但是,没有收到来自服务器的消息,也没有服务器捕获的消息。

这是我的日志:

Starting IoServer server on: 0.0.0.0:8080

someone connected

someone sent a message: GET /chat/?EIO=3&transport=polling&t=MWjzh2Z HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: */*
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
DNT: 1
Referer: http://localhost:8100/chat
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: XDEBUG_SESSION=XDEBUG_ECLIPSE; PHPSESSID=d3c24da76bc02d17a4ae8f7eadeabe07

someone has disconnected

任何想法出了什么问题?

0 个答案:

没有答案