可以';t在ws://localhost:8000/socket/server/startDaemon.php上建


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

我正在使用javascript连接websocket:

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

我得到了错误:

无法在上建立与服务器的连接

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

我该如何解决这个问题?

注意:我在mozilla中启用了websocket来支持websocket应用程序。当我在chrome中运行时,我得到了错误:

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

显然firefox4由于存在漏洞而禁用了websocket。引用本文:

WebSocket在Firefox 4中被禁用

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

我通过以下链接代码解决了我的错误

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

它工作得很好。

您想在Firefox中运行客户端吗?根据文件:

截至2010年2月支持的网络套接字是Google Chrome和Webkit Nightlies。从这里获取http://www.google.com/chrome

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

首先,您的错误是将php函数与javascript require_once 'WebSocket.php';一起使用,然后按照下面的链接来完成教程。

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

它运行良好。

谢谢,