简体   繁体   English

无法连接到websocket

[英]Unable to connect to a websocket

I created a PHP websocket using Ratchet . 我使用Ratchet创建了一个PHP websocket。 This Websocket is running on port 8080 of my internal server 10.0.4.160 . 此Websocket正在我的内部服务器10.0.4.160端口8080上运行。

I am trying to connect to it from a website that have SSL enabled "aka using https protocol." 我试图从一个启用了SSL“又名使用https协议”的网站连接到它。

When attempting to connect to the websocket form a FireFox browser, I get security issues from the browser because I am mixing between secured/non-secured connection. 当尝试从FireFox浏览器连接到websocket时,我从浏览器中得到安全问题,因为我在安全/非安全连接之间混合。 This is the error that I see in the javascript's console. 这是我在javascript控制台中看到的错误。

SecurityError: The operation is insecure. SecurityError:操作不安全。

To fix the security issue I created a stunnel proxy which allows me to accept the connection on 10.0.4.160:58443 and connect it to port 127.0.0.1:8080 . 为了解决安全问题,我创建了一个stunnel代理,它允许我接受10.0.4.160:58443连接并将其连接到端口127.0.0.1:8080 This secure tunnel should allow me to keep my connection to the websocket secured and bypass the browser's security check. 这个安全隧道应该允许我保持与websocket的连接,并绕过浏览器的安全检查。

However, every time I attempt to connect to the websocket I get an error 但是,每次我尝试连接到websocket时都会出错

WebSocket connection to 'wss://10.0.4.160:58443/' failed: Error in connection establishment: net::ERR_TIMED_OUT 与'wss://10.0.4.160:58443 /'的WebSocket连接失败:连接建立错误:net :: ERR_TIMED_OUT

This is the jQuery script that is used to connect to the websocket 这是用于连接websocket的jQuery脚本

<script>

    $(function(){
        if ("WebSocket" in window)
        {
            var conn = new WebSocket('wss://10.0.4.160:58443');

            conn.onopen = function(e) {
                console.log("Connection established!");
                showMessage('Connected', 0);
            };

            conn.onmessage = function(e) {
                console.log(e.data);
                showMessage(e.data, 1);
            };

            conn.onclose = function(e) {
                console.log(e.code);
                console.log(e.reason);
            };              

            conn.onerror = function(e) {
                console.log(e);
            };      

        } else {
            console.log('Your browser does not support Websocket!');
        }

        function showMessage(msg, append){
            if(append){
                $('#status').append('<br>' + msg);
                return;
            }

            $('#status').text(msg);
        }
    });

</script>

Here is my current stunnel configuration 这是我目前的stunnel配置

[websockets]
client = yes
accept = 58443
connect = 8080
verify = 2
CAfile = ca-certs.pem
debug = 7
OCSPaia = yes

How can I connect to the websocket from my browser? 如何从浏览器连接到websocket? Thank you 谢谢

read more about html5 websockets at http://www.html5rocks.com/en/tutorials/websockets/basics/ http://www.html5rocks.com/en/tutorials/websockets/basics/上阅读有关html5 websockets的更多信息

Socket.io is a popular web socket library read more at www.socket.io Socket.io是一个流行的Web套接字库,请访问www.socket.io

There is not enough information to diagnose your issue. 没有足够的信息来诊断您的问题。 Please provide a server log. 请提供服务器日志。

  • Does your server recognize a connection attempt? 您的服务器是否识别连接尝试?
  • Does your server handle the client server handshake? 您的服务器是否处理客户端服务器握手?

If your server recognizes a connection attempt then your issue is more than likely the handshake. 如果您的服务器识别出连接尝试,则您的问题很可能是握手。 There is not an issue with your JavaScript. 您的JavaScript没有问题。


EXTRA INFORMATION: 额外的信息:

another type of connection: (we will call this type 1) 另一种类型的连接:(我们将这种类型称为1)

|SERVER(PHP)| <--- CONNECTION --> |SERVER(websocket)|

what you are using: (we will call this type 2) 你在用什么:(我们称之为2型)

|BROWSER(client)| <----CONNECTION --> |SERVER(websocket)|

when using type 1 connections as type 2 connections you risk overflowing your main web server which will cause sever performance issues. 当使用类型1连接作为类型2连接时,您可能会溢出主Web服务器,这将导致服务器性能问题。 Web host like: Godaddy, Hostgator.. ect.. will terminate connections that are flooding their servers because they are seen as DDOS attacks. 网络主机如:Godaddy,Hostgator ......等将终止充斥其服务器的连接,因为它们被视为DDOS攻击。

I dont know what ratchet is, but to keep a server & client connection open PHP will need to stay open & this is NEVER NEVER NEVER EVER EVER a good idea. 我不知道棘轮是什么,但为了保持服务器和客户端连接打开PHP将需要保持打开,这绝不是永远不会有一个好主意。 Look into created a server in languages such as C# or C++. 研究用C#或C ++等语言创建服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM