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