我用棘轮做了一个网络聊天,它在我的电脑上工作得很好,如果我用命令行启动脚本,它就会运行,如果我在另一个浏览器中打开聊天的本地主机,它就能很好地工作,我可以和我的另一个浏览器交谈,然后,我感到很好奇,但我试着部署我的聊天,并与我的朋友分享它是否对每个人都有效,而不是对每个人都有效,只发送消息,没有人能看到,所以,我不知道websocket是否真的工作,或者问题是否是服务器主机,因为我使用的是2000,也许使用websockets不是很好。
来源: chat-server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ricardo\Socket\Chat;
require 'vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();资料来源: chat.php(/lib/Ricardo/Socket)
<?php
namespace Ricardo\Socket;
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();
}
}资料来源: script.js
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
//console.log("Connection established!");
};
conn.onmessage = function(e) {
// console.log(e.data);
showMessages('other', e.data);
};
//conn.send('Hello World!');
///////////////////////////////////////////////
var form1 = document.getElementById('chat');
var inp_message = document.getElementById('message');
var inp_name = document.getElementById('name');
var btn_env = document.getElementById('send');
var area_content = document.getElementById('content');
btn_env.addEventListener('click', function(){
if (inp_message.value != '') {
var msg = {'name': inp_name.value, 'msg': inp_message.value};
msg = JSON.stringify(msg);
conn.send(msg);
showMessages('me', msg);
inp_message.value = '';
}
});
function showMessages(how, data) {
data = JSON.parse(data);
console.log(data);
if (how == 'me') {
var img_src = "chat.png";
} else if (how == 'other') {
var img_src = "chat-1.png";
}
var div = document.createElement('div');
div.setAttribute('class', how);
var img = document.createElement('img');
img.setAttribute('src', img_src);
var div_txt = document.createElement('div');
div_txt.setAttribute('class', 'text');
var h5 = document.createElement('h5');
h5.textContent = data.name;
var p = document.createElement('p');
p.textContent = data.msg;
div_txt.appendChild(h5);
div_txt.appendChild(p);
div.appendChild(img);
div.appendChild(div_txt);
area_content.appendChild(div);
}嗯,我只发了这些消息,因为在我看来,这很重要,救命,拜托!
发布于 2022-01-21 17:11:45
显然,棘轮不工作在2000网络主机,因为它需要shell访问。
https://stackoverflow.com/questions/62565658
复制相似问题