为啥消息不能发送过去
为啥连接上了,点击button不能将消息发送过去?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>websocket</title>
</head>
<body>
<h1>Echo Test</h1>
<input type="text" id="sendTxt">
<button id="sendButton">发送</button> <!-- 点击会将发送的内容给server -->
<div id="recv"></div><!-- 浏览器返回的内容 -->
<script type="text/javascript">
var websocket=new WebSocket("ws://echo.websocket.org/");//建立socket
//连接建立的时候,会调用一个回调函数
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('sendButton').onclick=function(){
var txt=document.getElementById('sendTxt').value;
websocket.send(txt);
}
</script>
</body>
</html>