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

时间:2022-04-17 15:54:15

I am using javascript to connect websocket:

我使用javascript连接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:

注意:我在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);

4 个解决方案

#1


5  

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

显然,firefox 4因漏洞而禁用了websockets。引用来自这篇文章:

WebSocket disabled in Firefox 4

Firefox 4中禁用了WebSocket

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.

最近的发现发现Websocket使用的协议容易受到攻击。 Adam Barth演示了一些针对该协议的严重攻击,攻击者可以利用该攻击来毒害位于浏览器和Internet之间的缓存。

#2


3  

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

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;

        }

}

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

并在'WebSocketServer.php'的send函数中添加了代码,用于调用响应请求消息的'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));
    }

it's working great.

它工作得很好。

#3


1  

Are you trying to run the client in Firefox? According to the documentation:

您是否尝试在Firefox中运行客户端?根据文件:

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

截至2月10日,支持websockets的唯一浏览器是Google Chrome和Webkit Nightlies。从http://www.google.com/chrome获取它

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

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

#4


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.

首先你的错误是使用php函数和javascript require_once'WebSocket.php';其次,请阅读以下链接中的教程。

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

it's working fine.

它工作正常。

Thanks,

#1


5  

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

显然,firefox 4因漏洞而禁用了websockets。引用来自这篇文章:

WebSocket disabled in Firefox 4

Firefox 4中禁用了WebSocket

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.

最近的发现发现Websocket使用的协议容易受到攻击。 Adam Barth演示了一些针对该协议的严重攻击,攻击者可以利用该攻击来毒害位于浏览器和Internet之间的缓存。

#2


3  

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

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;

        }

}

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

并在'WebSocketServer.php'的send函数中添加了代码,用于调用响应请求消息的'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));
    }

it's working great.

它工作得很好。

#3


1  

Are you trying to run the client in Firefox? According to the documentation:

您是否尝试在Firefox中运行客户端?根据文件:

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

截至2月10日,支持websockets的唯一浏览器是Google Chrome和Webkit Nightlies。从http://www.google.com/chrome获取它

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

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

#4


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.

首先你的错误是使用php函数和javascript require_once'WebSocket.php';其次,请阅读以下链接中的教程。

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

it's working fine.

它工作正常。

Thanks,