2011-05-30 208 views
13

我使用JavaScript来连接的WebSocket:无法在ws:// localhost:8000/socket/server/startDaemon.php建立到服务器的连接。 var socket = new WebSocket(host);

<script> 
    var socket; 
    var host = "ws://localhost:8000/socket/server/startDaemon.php"; 
    var socket = new WebSocket(host); 
</script> 

我得到了错误:

Can't establish a connection to the server at

var host = "ws://localhost:8000/socket/server/startDaemon.php"; 
var socket = new WebSocket(host); 

我怎样才能解决这个问题呢?

注意:我启用了mozilla中的websocket来支持web套接字应用程序。 当我在Chrome上运行我得到错误:

can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host); 
+0

你是否尝试从cli运行startDaemon.php?并且startDaemon.php应该包含监听端口8000. – Sujeet 2011-06-08 06:47:03

+0

首先你分离客户端和服务器端代码。 :) – Sujeet 2011-06-08 09:05:35

回答

3

我通过这个链接

http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/ 下面的代码解决了我的错误,并创建socketWebSocketTrigger.class.php文件响应消息,其中代码

class socketWebSocketTrigger 
{ 

     function responseMessage($param) 
     { 
      $a = 'Unknown parameter'; 

      if($param == 'age'){ 
       $a = "Oh dear, I'm 152"; 
      } 

      if($param == 'hello'){ 
       $a = 'hello, how are you?'; 
      } 

      if($param == 'name'){ 
       $a = 'my name is Mr. websocket'; 
      } 

      if($param == 'today'){ 
       $a = date('Y-m-d'); 
      } 

      if($param == 'hi'){ 
       $a = 'hi there'; 
      } 

      return $a; 

     } 

} 

,并添加代码在'WebSocketServer.php'的发送函数中调用'responseMessage'函数,该函数响应请求消息

public function send($client, $msg){ 
     $this->say("> ".$msg); 
     $messageRequest = json_decode($msg,true); 

      // $action=$messageRequest[0]; 
      $action = 'responseMessage'; 
      $param = $messageRequest[1]['data']; 
     if(method_exists('socketWebSocketTrigger',$action)){ 
           $response = socketWebSocketTrigger::$action($param); 
          } 
      $msg = json_encode(
       array(      
       'message', 
        array('data' => $response) 
       ) 
      ); 

      $msg = $this->wrap($msg); 

     socket_write($client, $msg, strlen($msg)); 
    } 

它工作得很好。

+0

我很抱歉,但你在这个代码中使用新的websocket? – Ibu 2011-06-08 22:19:19

+0

请从上面的链接看我已经采取代码并从命令行运行第一个server.php然后运行索引文件。您可以得到结果。 – 2011-06-09 04:59:48

5

显然火狐4的禁用,因为漏洞的WebSockets。引述这篇文章:

WebSocket disabled in Firefox 4

Recent discoveries found that the protocol that Websocket works with is vulnerable to attacks. Adam Barth demonstrated some serious attacks against the protocol that could be used by an attacker to poison caches that sit in between the browser and the Internet.

+0

浏览器不是问题,我启用websocket在Firefox中。可能是在端口或套接字的问题。 – 2011-06-08 04:03:40

1

你们是不是要运行在Firefox的客户端?据the documentation

As of Feb/10 the only browsers that support websockets are Google Chrome and Webkit Nightlies. Get it from here http://www.google.com/chrome

尝试在Chrome中运行它,看看是否适合你。

1

首先,您的错误是使用php函数与javascript require_once 'WebSocket.php';,其次通过教程,如下面的链接。

http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

它的正常工作。

感谢,

+0

我纠正了它,但该包的websocket在我的系统中工作不正常。您是否在聊天中发送请求消息时收到消息?我仍然面临着这个错误。先谢谢你。 – 2011-06-08 09:47:35