天天看點

伺服器向浏覽器推送

1.comet(伺服器向頁面推送資料) 單向

伺服器向浏覽器推送

2.SSE:圍繞comet推出的API

var source=new EventSource("myevent.php");//url必須與建立對象的頁面同url,域和端口

source.onmessage=function(ev){

     ev.data......

}      

3.web socket 全雙工雙向通信 建立連接配接後會把http協定更新為web socket協定,需要自己的伺服器,http伺服器無法滿足  

var socket=new WebSocket("ws://www.example.com/server.php");//url為絕對路徑,不存在同不同源問題

socket.send(JSON.stringfy(json1));//發送資料必須為字元串

socket.onmessage=function(ev){

     ev.data......

}      

轉載于:https://www.cnblogs.com/shytong/p/4959237.html