您的位置:首页 > 其它

Websocket 入门学习实例

2017-09-08 09:24 176 查看
<!DOCTORY html>
<html>
<head>
<meta charset="utf-8" />
<title>Websocket</title>
</head>
<body>
<h1>Echo Test</h1>
<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>

div.
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  websocket