首页 > Web开发 > 详细

一个简单的websocket

时间:2017-11-17 14:37:54      阅读:221      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input id="sendTxt" type="text">
<button id="sendBtn">发送</button>
<div id="recv"></div>
<script type="text/javascript">
var websocket = new WebSocket("ws://echo.websocket.org/");
websocket.onopen = function(){
console.log(‘websocket open‘);
document.getElementById("recv").innerHTML = "Connected";
}
websocket.onclose = function(){
console.log("websocket close");
}
websocket.onmessage = function(e){
console.log(e.data);
document.getElementById("recv").innerHTML = e.data;
}
document.getElementById("sendBtn").onclick = function()
{
var txt = document.getElementById("sendTxt").value;
websocket.send(txt);
}
</script>
</body>
</html>

一个简单的websocket

原文:http://www.cnblogs.com/lvqi/p/7851437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!