2011-05-30 22 views
13

estoy usando javascript para conectar WebSocket:No se puede establecer una conexión con el servidor en 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> 

Tengo el error:

Can't establish a connection to the server at

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

¿Cómo puedo solucionar este problema?

NOTA: habilité websocket en mozilla para que sea compatible con la aplicación de socket web. y cuando corro en cromo llegué error:

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

¿intentó ejecutar startDaemon.php desde cli? y startDaemon.php debe contener que escucha el puerto 8000. – Sujeet

+0

primero separa el lado del cliente y el código del lado del servidor. :) – Sujeet

Respuesta

3

I resuelto mi error siguiente código a través de este enlace

http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/ y crearon archivo socketWebSocketTrigger.class.php de mensaje de respuesta en código como

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; 

     } 

} 

y el código añadido en la función de envío de 'WebSocketServer.php' para llamar a la función 'responseMessage', mensaje de solicitud de respuesta

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)); 
    } 

funciona a la perfección.

+0

lo siento, pero ¿dónde utiliza el nuevo websocket en este código? – Ibu

+0

Mire una vez el enlace de arriba del cual he tomado el código y ejecute primero server.php desde la línea de comando y luego ejecute el archivo de índice. Puede obtener el resultado. –

5

Al parecer Firefox 4 tiene websockets discapacitadas debido a vulnerabilidades. Para citar este artículo:

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

El navegador no es problema, habilité websocket en firefox. Puede haber un problema en el puerto o en el socket. –

1

¿Está intentando ejecutar el cliente en Firefox? De acuerdo con 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

intente ejecutar en Chrome y ver si funciona para usted.

1

En primer lugar su error es utilizar la función php con javascript require_once 'WebSocket.php'; y en segundo lugar, seguir el tutorial como en el siguiente enlace.

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

que está funcionando bien.

Gracias,

+0

Lo corrigí pero ese paquete para websocket no funciona bien en mi sistema. ¿Recibió recibir mensaje cuando envió un mensaje de solicitud en el chat? Todavía estoy enfrentando ese error. gracias de antemano. –

Cuestiones relacionadas