xsnow
机遇总是留给有准备的人

一、官网

官网地址:http://www.websocket.org/

二、websocket初识demo

<input id="sendTxt" type="text"/>
<button id="sendBtn">发送</button>
<div id="recv"></div>

<script type="text/javascript">
var ws = new WebSocket("ws://echo.websocket.org/");
ws.onopen = function () {
  console.log("websocket open");
  document.getElementById("recv").innerHTML = "connected";
}
ws.onclose = function () {
  console.log("websocket close");
  document.getElementById("recv").innerHTML = "closed";
}
ws.onmessage = function (e) {
  console.log(e.data);
  document.getElementById("recv").innerHTML = e.data;
}
document.getElementById("sendBtn").onclick = function () {
  var txt =  document.getElementById("sendTxt").value;
  ws.send(txt);
}
</script>

 

posted on 2019-04-02 09:48  xsnow  阅读(108)  评论(0编辑  收藏  举报