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..

10
  • I would recommend you to use a real socket like socket.io to provide a real socket connection Aug 14, 2016 at 16:23
  • @Jonasw, but I guess that need node,js?
    – 112233
    Aug 14, 2016 at 16:24
  • Em /socketserver is a folder? Aug 14, 2016 at 16:36
  • I don't think so, creating such a folder, gives me this error: Unexpected response code: 301
    – 112233
    Aug 14, 2016 at 16:40
  • 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. Aug 14, 2016 at 18:55

1 Answer 1

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.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.