1

I'm trying to use websocket with php and js. I referred to many online resources to understand how the connection is established. But I just can't understand how to specify the url.

I learnt from one of the resources I referref to, that URL is the server that I want to connect to.

My site structure now is this:

root/test/test.js

in the test.js, my code to make connection is this:

jQuery(document).ready(function($) {
 WebSocketTest();
  function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");

               // Let us open a web socket
               var ws = new WebSocket("ws://mysite.localhost.com");

               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };

               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };

               ws.onclose = function()
               { 
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };
            }

            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }

 }
});

When I run the above code, it says..

1) websocket is supported by your browser

2) connection is closed...

3) in the console, WebSocket connection to 'ws://mysite.localhost.com/' failed: Error during WebSocket handshake: Unexpected response code: 200

Please explain to me only how to establish connection and to which file or server. Thanks in advance...

here..https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

this is the URL given for example.Can someone explain what that /socketserver means

var exampleSocket = new WebSocket("ws://www.example.com/socketserver", "protocolOne");

Connection is made, if connected to ws://echo.websocket.org/, but I'm not clear on this concept..

112233
  • 2,406
  • 3
  • 38
  • 88
  • I would recommend you to use a real socket like http://socket.io to provide a real socket connection – Jonas Wilms Aug 14 '16 at 16:23
  • @Jonasw, but I guess that need node,js? – 112233 Aug 14 '16 at 16:24
  • Em /socketserver is a folder? – Jonas Wilms Aug 14 '16 at 16:36
  • I don't think so, creating such a folder, gives me this error: Unexpected response code: 301 – 112233 Aug 14 '16 at 16:40
  • http 301 means redirect means /socketserver is redirected to /socketserver/ – Jonas Wilms Aug 14 '16 at 16:44
  • Jonas, I'm not getting the idea...just creating an empty folder has no use? How to really make this conection.. – 112233 Aug 14 '16 at 16:46
  • You cannot do async stuff with php. You should read the wikipedia articles websocket, php, and a lot more – Jonas Wilms Aug 14 '16 at 16:49
  • 2
    200 is the response code sent by an HTTP server as a success reply to a requested resource. A WebSocket starts with a standard HTTP request that "upgrades" the connection to the WebSocket protocol. The upgrade success response code is 101. If you are getting 200 instead, it means the URL you are trying to connect to does not recognize WebSocket connections. – Remy Lebeau Aug 14 '16 at 18:55
  • @RemyLebeau, I found that if the url specified to this: ws://echo.websocket.org, connection s made..How do I make this connection local instead of connecting to 3rd party server? – 112233 Aug 15 '16 at 01:42
  • @Keren obviously, you need to run a local server that supports WebSockets. – Remy Lebeau Aug 15 '16 at 01:54

1 Answers1

0

Websocket requires you to create a endpoint on server side to etablish the connection.

But i can only see the js part of your code here, did you ever made a server endpoint for your websocket connection?

In "ws://www.example.com/socketserver" , the socketServer means the name of websocket server endpoint.

So you need to do something in server side to make it work.

linxuhao
  • 1
  • 1
  • i dont know how to make a websocket server on php, but there is a link in java to help you understand the concept https://stackoverflow.com/questions/26452903/javax-websocket-client-simple-example – linxuhao May 01 '18 at 12:39