如何发送和使用棘轮

问题描述:

这是我第一次使用套接字,并与起步棘轮但我不能认真地我的头安装到它的服务器获取数据。仍尝试将教程中的一些部分连接在一起,但面临一些问题。另外,我想知道如何使用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的\聊天:: 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'; 
     } 


}); 

我知道这些都是一个很大的问题,但我真的想知道怎么办。如果有任何一步一步简单的学习教程,这些也是受欢迎的。

+0

你的第二个问题是不明确的。请显示你的'html'并告诉你你想做什么以及你得到了什么。 – spirit

+0

,并告诉我们,** **怎么没你想使用** ** autobahn.js?做什么的? – spirit

+0

据它说,使用autobahn.js的客户端,但我无法弄清楚如何使用的文档。而对于第二个问题,我问的是,当一个客户发送一条消息时,他自己会看到他在聊天区发送的内容。那么该怎么做。 – Ayan

如果你想在Rachet site改变例如从tutorial,比你的第一个问题的解决方案是:

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代码

,修改下一个方法:

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>"); 
    } 
}; 
+0

是的,我想出了两分钟前看到你以前的答案。但事情是当一个新用户加入正在发送的正在发送的正在加入的消息时,它将在onmessage下发送。是否有可能打开onopen?所以,当加入发生在不同的班级名单下?我希望我明确自己的意思? – Ayan

+0

你可以看到在onOpen下的Js我有类似于127.0.0.1的东西,我想在那里得到连接消息,这样每次用户加入或离开时,他们都会在该特定的css类下获得消息。 – Ayan

+0

我改变了在php部分的onOpen下的一行到$ client-> send({$ conn-> remoteAddress}'online');但为什么它不工作? – Ayan