2016-05-11 13:50



I'm trying to make a chat using WebSocket. In that purpose I use Ratchet.

For that, I'm using the guide from Ratchet :

My problem is that after sending a message, the page reloads without any implementation of code for it.

index.html :

<html> <!-- index.html -->
    <h2>Create a chat server on port 8080</h2>
    <button><a href="chat-server.php">Here</a></button>
    <h2>Join the chat server on port 8080</h2>
    <button><a href="chat-client.php">Here</a></button>
    <p>You'll have an error if it doesn't exist !</p>


var conn;
function init(){
   console.log("function : init");
   conn = new WebSocket('ws://localhost:8080');
   conn.onopen = function(e) {
      var co = document.getElementById("connection");
      co.innerHTML="Connection established !";
conn.onmessage = function(e) {
    var content = document.getElementById("chat");
    content.innerHTML = content.innerHTML + "<li>"+"</li>";
conn.onclose = function(){
    var co = document.getElementById("connection");
    co.innerHTML="Connection closed !";
conn.onerror = function(){
    alert("Connection failed : There in no server on this port !");
function closeCon(){
function sendMessage(){
    var mes = document.getElementById("message").value;
    conn.send(mes, function(event){
    } );

chat-client.php :

<html><!-- chat-client.php -->
    <script src="../js/connection.js"></script>
<body onload="init()">
<h1>Chat in web browser</h1>
<p id="connection"> Connection closed !</p>
    <ul id="chat">

        <input id="message" style="border: 1;">
        <button onclick="sendMessage()">Send</button>
<button onclick="closeCon()"><a href="index.html">Back to the menu</a></button>

chat-server.php :

<h1>Welcome on your server on port 8080</h1>

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

// cd /Applications/MAMP/htdocs/MyRatchetFirstApp/
require dirname(__DIR__) . '/vendor/autoload.php';

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

Chat.php :

<?php //Chat.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
        echo "New connection! ({$conn->resourceId})<br/>";

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

    public function onClose(ConnectionInterface $conn) {
    // The connection is closed, remove it, as we can no longer send it messages
        echo "Connection {$conn->resourceId} has disconnected<br/>";
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}<br/>";

composer.json :

    "autoload": {
        "psr-0": {
            "MyApp": "src"
    "require": {
        "cboden/ratchet": "0.3.*"

And finally, here,the tree of this project

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doujiao1984 doujiao1984 5年前

    That was just the button to send the message was in a form. Then when I "submit" it, it refreshed the page. If you want to have an history of the posts, you should remove this form.

    点赞 评论 复制链接分享