WebSockets

Un lucru bun in legatura cu Ajax era posibilitatea de a face modificari din mers, insa dezavantajul era ca nu puteati sa transmiteti dupa dorinta informatia pornind de la server. Cu ajutorul WebSockets obtinem comunicare bidirectionala reala: putem atat sa trimitem, cat si sa primim date.

//create a new Web Socket. Note the use of the ws:// protocol

var ws=new WebSocket(“ws://robertnyman.com/wsmagic”);

//send data

ws.send(“Some data”);

//when the connection is opened

ws.onopen=function() {

console.log(“Conection Opened!”);

};

//when you receive a message pushed from the server

ws.onmessage=function(evt) {

console.log(evt.data);

};

//close the connection

ws.close();

//whe you close the conection

ws.onclose=function() {

console.log(“connection closed”):

};

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>