2017-08-28 144 views
0

我一直在嘗試在本地主機上配置Ratchet,並且一直遵循this tutorialJavascript無法連接到PHP棘輪WebSocket服務器

我已經安裝了Composer和Ratchet,並且完全從該教程中複製了PHP代碼。當我運行服務器並使用telnet來訪問它時,我沒有問題,它工作正常。

但是,當我嘗試使用JavaScript來建立連接(使用HTML5 websockets)時,它不會連接 - 請求在一段時間後簡單超時。我可以看到我的瀏覽器在PHP控制檯和telnet中發送的初始HTTP請求消息,因此客戶端顯然可以「很好地連接」 - 就好像服務器沒有確認這個請求。

我事先在StackOverflow和其他類似網站上看到了其他人的問題,有些人提到服務器必須發回一個HTTP回覆,我嘗試過這樣做(對最近連接的客戶端使用send方法如果他們的消息以GET HTTP/1.1開始)。我查閱了MDN上的一些規範,並找到了this guide,但是我的實現對此問題沒有影響 - JavaScript仍然無法連接。我不確定是否這是因爲我錯誤地實現了握手代碼,或者它不是我最初的問題的解決方案。

WebSocket + Ratchet指南沒有提到需要實現這一點,所以我懷疑這可能不是問題。

我已經嘗試了兩個端口80808888,並且都有相同的結果。我在谷歌瀏覽器使用XAMPP在MacOS 60

這裏是我的JavaScript代碼:

window.onload = function() { 
    var conn = new WebSocket('ws://localhost:8080'); 
    conn.onmessage = function(e) { 
     console.log(e.data); 
    } 
    conn.onopen = function(e) { 
     console.log("Connection established!"); 
    } 
} 

這是我的PHP服務器的代碼(bin/chat-server.php):

<?php 
use Ratchet\Server\IoServer; 
use MyApp\Chat; 

require dirname(__DIR__) . '/vendor/autoload.php'; 

$server = IoServer::factory(
    new Chat(), 
    8080 
); 

$server->run(); 

這裏是聊天類(src/MyApp/Chat.php):

<?php 
namespace MyApp; 
use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 

class Chat implements MessageComponentInterface { 
    protected $clients; 

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

    public function onOpen(ConnectionInterface $conn) { 
     // Store the new connection to send messages to later 
     $this->clients->attach($conn); 

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

    } 

    public function onMessage(ConnectionInterface $from, $msg) { 
     $numRecv = count($this->clients) - 1; 
     echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n" 
      , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'); 
     foreach ($this->clients as $client) { 
      if ($from !== $client) { 
       // The sender is not the receiver, send to each client connected 
       $client->send($msg); 
      } 
     } 
    } 

    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(); 
    } 
} 

回答

0

因此,您不需要執行手搖,棘輪爲你做。

所有你需要做的就是這樣的代碼我有以上的工作是確保你把它使用一個Web服務器,像這樣:

<?php 
use Ratchet\Server\IoServer; 
use Ratchet\Http\HttpServer; 
use Ratchet\WebSocket\WsServer; 
use MyApp\Chat; 

require dirname(__DIR__) . '/vendor/autoload.php'; 

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

$server->run();