Hello World Tutorials can be only connected by telnet instead of browsers.

51 views
Skip to first unread message

skyl...@gmail.com

unread,
Dec 7, 2015, 12:45:36 PM12/7/15
to Ratchet
the server code:

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/12/7
* Time: 10:01
*/

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

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/12/7
* Time: 10:06
*/

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

require __DIR__ . '/../vendor/autoload.php';

$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
10023,"192.168.1.110"
);

$server->run();

==========================================
the client code fragment:
...
<script>
var conn = new WebSocket('ws://192.168.1.110:10023');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};
</script>
....

telnet 192.168.1.110 10023 ---connection success!

i open the client.html by FireFox browser , but connection fail!

claytonat...@gmail.com

unread,
Dec 9, 2015, 1:05:49 PM12/9/15
to Ratchet
Same here - I cannot connect with either Chrome or FF.  In both cases, the connection works, and on the server there is a message that the connection succeeds, but when conn.send('whatever') is invoked, the server disconnects the client.

claytonat...@gmail.com

unread,
Dec 9, 2015, 1:19:25 PM12/9/15
to Ratchet
Same here - I cannot connect with either Chrome or FF.

Error on Chrome: failed:    Invalid frame header
Error on FF:                     The connection to ws://... was interrupted while the page was loading.

In both cases, server-side shows a connect, and disconnect when message sent.


On Monday, December 7, 2015 at 10:45:36 AM UTC-7, 鲁昊 wrote:
Reply all
Reply to author
Forward
0 new messages