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

发布于 2024-11-10 16:56:20 字数 670 浏览 2 评论 0原文

我正在使用 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 以支持 Web 套接字应用程序。 当我在 chrome 中运行时出现错误:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事↓在人 2024-11-17 16:56:20

显然 Firefox 4 由于漏洞而禁用了 websocket。引用本文:

WebSocket 在 Firefox 4 中禁用

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

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.

墨洒年华 2024-11-17 16:56:20

我通过此链接跟踪代码

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

效果很好。

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.

辞别 2024-11-17 16:56:20

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

截至 2010 年 2 月,唯一的浏览器
支持 websocket 的是 Google Chrome
和 Webkit Nightlies。从这里获取
http://www.google.com/chrome

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

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.

千鲤 2024-11-17 16:56:20

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

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

工作正常。

谢谢,

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文