gpt4 book ai didi

php - 如何使用 Ratchet 从服务器发送和获取数据

转载 作者:行者123 更新时间:2023-12-04 16:13:47 25 4
gpt4 key购买 nike

这是我第一次使用套接字并从 开始 Ratchet 但我真的无法适应它。仍然尝试将教程中的某些部分连接在一起,但面临一些问题。另外我想知道如何使用 autobahn.js 用它。教程不是很清楚。

我的问题

1) 如何向除当前用户之外的所有用户发送消息,声明“
...加入”并且'...'必须是用户的IP。

我尝试了以下操作,但它在终端上给了我错误。

public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
$this->send('new');
echo "New connection! ({$conn->remoteAddress})\n";
}

Call to undefined method MyApp\Chat::send()



2) 怎样做才能在发送消息时所有各方都可以查看它,包括发送它的人(这就是每个聊天的工作方式)?

JS
var conn = new WebSocket('ws://localhost:8080');

conn.onopen = function(e) {
//console.log("Connection established!");
$("#chat_window #messages_list").append("<li class='join_disconnect'>127.0.0.1 Online</li>");
}
conn.onmessage = function(e) {
//console.log(e.data);
$("#chat_window #messages_list").append("<li class='thread'>"+e.data+"</li>");
}
conn.onclose = function(e) {
//console.log("Connection Disconnected!");
$("#chat_window #messages_list").append("<li class='join_disconnect'>127.0.0.1 Offline</li>");
};



$(document).ready(function(){

disable_button();


//EMERGENCY EXIT
$('#exit').click(function(){
window.location.replace('http://www.google.com');
});


//PREVENT BLANK INPUT
$('#msg').on('keyup', function(e){

if($(this).val().length>0){
enable_button();
}
else{
disable_button();
}

});

//SEND MESSAGE
$('#snd').click(function(){
var thread = $('#msg').val();
//console.log(thread);
//conn.send(thread);
$.ajax({
type:'POST',
url: './bin/send-message.php',
data: {msg: thread},
success: function(response){
//alert(response);
if(response!=1){
$('#msg').val('');
disable_button();
}
}
});
});

//ENABLE BUTTON
function enable_button() {
var element = document.getElementById('snd');
$(element).addClass('active');
element.style.pointerEvents = '';
}

//DISABLE BUTTON
function disable_button() {
var element = document.getElementById('snd');
$(element).removeClass('active');
element.style.pointerEvents = 'none';
}


});

我知道这些问题很多,但我真的很想知道如何做。如果有任何易于学习的分步教程,也欢迎使用。

最佳答案

如果您尝试更改 tutorial 中的示例在 Rachet site ,比你的第一个问题解决方案是:

public function onOpen(ConnectionInterface $conn) {
// first, you are sending to all existing users message of 'new'
foreach ($this->clients as $client) {
$client->send('new');
}
// than,
// 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) {
// remove this check
// if ($from !== $client) {
// The sender is not the receiver, send to each client connected
// $client->send($msg);
// }

// just send to all
$client->send($msg);
}
}

更新 : 完整的解决方案。

Chat.php ,需要修改一些方法:
public function onOpen(ConnectionInterface $conn) {
// first, you are sending to all existing users message of 'new'
foreach ($this->clients as $client) {
$client->send('<status>' . $conn->remoteAddress . ' Online</status>'); //here we are sending a status-message
}
// than,
// Store the new connection to send messages to later
$this->clients->attach($conn);

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

public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);

//send to others clients message about disconnected user
foreach ($this->clients as $client) {
$client->send('<status>' . $conn->remoteAddress . ' Offline</status>'); //here we are sending a status-message too
}

echo "Connection {$conn->remoteAddress} has disconnected\n";
}

在您的 js代码,修改next方法:
conn.onmessage = function(e) {
//console.log(e.data);
var match = e.data.match(/^<status>(.*?)<\/status>$/i);
if (match) {
if (/\d+\.\d+\.\d+\.\d+ online/i.test(match[1])) {
messages.append('<li class="join_connect">' + match[1] + "</li>");
} else if (/\d+\.\d+\.\d+\.\d+ offline/i.test(match[1])) {
messages.append('<li class="join_disconnect">' + match[1] + "</li>");
}
} else {
messages.append('<li class="thread">' + e.data + "</li>");
}
};

关于php - 如何使用 Ratchet 从服务器发送和获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com