What is WebSockets in HTML5?

WebSockets in HTML5

Web Sockets is a next-generation communication technology for web applications by which you can send or receive data from server like ajax get post method.

You have already used like this type of code in jQuery

$.post() Or $.get() or $.ajax() etc

What these function do?
These function used for send and receive data from server to client or client to server.

Same as WebSockets works.

Syntax: var Socket = new WebSocket(url, [protocal] );

Example:

<script type="text/javascript">
function WebSocketCheck()
{
if("WebSocket" in window)
{
 alert("WebSocket is supported by your Browser!");
 // Lets open a web socket 
 var ws = new WebSocket("www.url.com/para"); 
 ws.onopen = function() 
 {
 // Web Socket is connected now, send data using send() Method
 ws.send("Message to send server");
 alert("Message is sent now.");
 };
 ws.onmessage = function (evt) {
 var received_msg = evt.data;
 alert("Message is received by server");
 };
 ws.onclose = function() {
 // websocket is closed. 
 alert("Connection is closed from server");
 }; 
}
</script>
<div id="sse"> <a href="javascript:WebSocketCheck()">Run WebSocket</a> </div>

Comments