Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的WebSocket客户端总是在发送消息后重新加载?_Javascript_Php_Websocket_Chat_Ratchet - Fatal编程技术网

Javascript 为什么我的WebSocket客户端总是在发送消息后重新加载?

Javascript 为什么我的WebSocket客户端总是在发送消息后重新加载?,javascript,php,websocket,chat,ratchet,Javascript,Php,Websocket,Chat,Ratchet,我想用WebSocket聊天。为此,我使用棘轮 为此,我使用了Ratchet的指南: 我的问题是,在发送消息之后,页面会重新加载,而没有任何代码实现 index.html: <html> <!-- index.html --> <head> </head> <body> <h1>Menu</h1> <h2>Create a chat server on port 8080</h

我想用WebSocket聊天。为此,我使用棘轮

为此,我使用了Ratchet的指南:

我的问题是,在发送消息之后,页面会重新加载,而没有任何代码实现

index.html:

<html> <!-- index.html -->
<head>
</head>
<body>
    <h1>Menu</h1>
    <h2>Create a chat server on port 8080</h2>
    <button><a href="chat-server.php">Here</a></button>
    <hr>
    <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>
</body>
</html>

最后,在这里,

只是发送消息的按钮,它是一个表单。然后当我“提交”它时,它刷新了页面。如果您想拥有帖子的历史记录,您应该删除此表单。

这只是发送消息的按钮,它是表单中的一个按钮。然后当我“提交”它时,它刷新了页面。如果您想拥有帖子的历史记录,您应该删除此表单。

您应该内联添加页面代码。那么,单击按钮后是否会重新加载页面?或者在将消息发送到套接字后?是的,它在发送消息后重新加载页面。我们可以注意到,因为其他客户端收到了消息。您需要使用CMD运行“chat server.php”。如果您在服务器上。您可以使用后台进程,如“管理”或“您应该内联添加页面代码”。那么,单击按钮后是否会重新加载页面?或者在将消息发送到套接字后?是的,它在发送消息后重新加载页面。我们可以注意到,因为其他客户端收到了消息。您需要使用CMD运行“chat server.php”。如果您在服务器上。您可以使用后台进程,如Supervisor
var conn;
function init(){
   console.log("function : init");
   conn = new WebSocket('ws://localhost:8080');
   console.log(conn);
   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>"+ e.data+"</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(){
    conn.close();
}
function sendMessage(){
    var mes = document.getElementById("message").value;
    conn.send(mes, function(event){
        event.preventDefault();
        console.log(event);
    } );
}
<html><!-- chat-client.php -->
<head>
    <script src="../js/connection.js"></script>
</head>
<body onload="init()">
<h1>Chat in web browser</h1>
<p id="connection"> Connection closed !</p>
<div>
    <ul id="chat">

    </ul>
    <form>
        <input id="message" style="border: 1;">
        <button onclick="sendMessage()">Send</button>
    </form>
</div>
<hr>
<button onclick="closeCon()"><a href="index.html">Back to the menu</a></button>
</body>
</html>
<h1>Welcome on your server on port 8080</h1>

<?php
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()
        )
    ),
    8080
);
$server->run();
<?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
        $this->clients->attach($conn);
        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
                $client->send($msg);
            }
        }
        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
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected<br/>";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}<br/>";
        $conn->close();
    }
}
    {
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "0.3.*"
    }
}