0

I have a PHP site that generates some HTML and Javascript. The Javascript initializes a Websocket and sends a message to the server after onopen fires. It works locally, but over the internet the connection is never established.

I'm trying to pin down the error. I have connected via websocket to my server across the internet from a plain html site with Javascript before. Is there any security mechanism against websockets in PHP sites or something? Here's the code briefly, can't see why it shouldn't work (especially since it works locally):

<?php
    echo "<!DOCTYPE html>
          <html>
              <head>
                  <script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\"></script>
                  <script>window.jQuery || document.write('<script src=\"js/vendor/jquery-1.11.0.min.js\"><\/script>');</script>
              </head>
              <body>
                  <script>
                      var socket;
                      var host = \"ws://xx.xxx.xx.xxx:1237/websocket\";  

                       $(document).ready(function()
                       {            
                           connect();  
                       });  

                       function connect()
                       {  
                            socket = new WebSocket(host); 
                            socket.onopen = function()
                            {
                                  // never fires
                            };  
                        }
                   </script>
              </body>
          </html>";
?>
4
  • did you check the view source of the php page to see if this is echoed properly? Commented Mar 4, 2014 at 7:51
  • <head>...</head> can I assume that you have linked the jquery js file inside your head tag> Commented Mar 4, 2014 at 7:52
  • @SajithNair Yes it is echoed properly, sadly. And I do have a jquery js linked. Commented Mar 4, 2014 at 8:01
  • Okay I'm pretty certain now that the solution is very simple, namely a port not being open. Thanks for your help, will report back in a bit to confirm my suspicion. Commented Mar 4, 2014 at 8:18

2 Answers 2

1

Sorry for wasting your time. I hadn't opened a port.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like jQuery js file is NOT linked in your <head> tag.

Following code will not work without jQuery

$(document).ready(function()
{            
    connect();  
});

1 Comment

I'm sorry, I shouldn't have left that out. I do have it linked. ready() fires, I have an alert() in there that gets called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.