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

4

16

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);
Maddie answered 30/5, 2011 at 4:48 Comment(2)
did u try running startDaemon.php from cli? and startDaemon.php should contain that listens to port 8000.Pineapple
first you separate client side and server side code. :)Pineapple
M
2

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.

Maddie answered 8/6, 2011 at 10:28 Comment(2)
i am sorry but where do u use the new websocket in this code?Coze
Please look once above link from which i have taken code and run first server.php from command line then run index file.You can get result.Maddie
C
5

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.

Coze answered 2/6, 2011 at 5:45 Comment(1)
Browser is not problem,I enabled websocket in firefox .May be problem in port or socket.Maddie
M
2

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.

Maddie answered 8/6, 2011 at 10:28 Comment(2)
i am sorry but where do u use the new websocket in this code?Coze
Please look once above link from which i have taken code and run first server.php from command line then run index file.You can get result.Maddie
G
1

Are you trying to run the client in Firefox? According to 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

Try running it in Chrome and see if that works for you.

Gaylenegayler answered 2/6, 2011 at 5:51 Comment(0)
P
1

First of all your mistake is using php function with javascript require_once 'WebSocket.php'; and secondly go through the tutorial as in the link below.

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

it's working fine.

Thanks,

Pineapple answered 8/6, 2011 at 9:9 Comment(1)
I corrected it but that package for websocket is not working fine in my system.Did you get receive message when send request message in chat? I am still facing that error. thank you in advance.Maddie

© 2022 - 2024 — McMap. All rights reserved.