【发布时间】:2016-07-09 11:41:15
【问题描述】:
这是我第一次使用套接字并从 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";
}
调用未定义的方法 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';
}
});
我知道这些问题很多,但我真的很想知道怎么做。如果有任何一步一步容易学习的教程,也欢迎。
【问题讨论】:
-
你的第二个问题不清楚。请出示您的
html,并告诉您您想做什么以及得到了什么。 -
并告诉我们,如何您想使用 autobahn.js?干什么用的?
-
根据文档,它说要在客户端使用 autobahn.js,但我不知道如何使用它。对于第二个问题,我问的是当客户发送消息时,他自己可以看到他在聊天区域发送的内容。那么该怎么做呢。
-
用你的第二个问题更新了我的答案。关于
autobahn.js,是的,它应该在客户端运行。但是你想使用它的原因是什么?你的目标是什么? -
我对这个库和如此多的依赖项完全感到困惑,真的不知道我在做什么。
标签: php websocket autobahn ratchet