天天看点

服务器向浏览器推送

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