Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using javascript to connect websocket:

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

I got the error:

Can't establish a connection to the server at

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

How can I solve this issue?

NOTE : I enabled websocket in mozilla to support web socket application. and when i run in chrome i got error:

   can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
like image 978
Sanjeev Kumar Jha Avatar asked May 30 '11 04:05

Sanjeev Kumar Jha


2 Answers

Apparently firefox 4 has websockets disabled because of vulnerabilities. To quote From this article:

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.

like image 181
Ibu Avatar answered Oct 06 '22 04:10

Ibu


I solved my error by following code through this link

http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/ and created socketWebSocketTrigger.class.php file for response message where code as

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;

        }

}

and added code in send function of 'WebSocketServer.php' for calling 'responseMessage' function which response request message

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

it's working great.

like image 35
Sanjeev Kumar Jha Avatar answered Oct 06 '22 04:10

Sanjeev Kumar Jha