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

San*_*Jha 14 php html5 websocket phpwebsocket

我使用javascript连接websocket:

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

我收到了错误:

无法与服务器建立连接

var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

注意:我在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);
Run Code Online (Sandbox Code Playgroud)

Ibu*_*Ibu 5

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

Firefox 4中禁用了WebSocket

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


San*_*Jha 2

我通过此链接跟踪代码解决了我的错误

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;

        }

}
Run Code Online (Sandbox Code Playgroud)

并在“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));
    }
Run Code Online (Sandbox Code Playgroud)

效果很好。