WebSocket长连接

2018-01-09  本文已影响17人  尤樊容

为了不让ajax一直发请求,为了节约资源,用了WebSocket,来做一下笔记:

//WebSocket长连接
var webSocket = new WebSocket("ws://"+ ip +":8080");
webSocket.onopen = function(event) {
  console.log("Connect open!");
  webSocket.send('hello');
  webSocket.onmessage = function(event) {
  //处理代码
  webSocket.close();
 }
};
};
webSocket.onerror = function(event) {
alert(event.data);
};
webSocket.onclose = function(event) {
 console.log("Connect close!");
}; 
上一篇下一篇

猜你喜欢

热点阅读