为啥消息不能发送过去

来源:2-1 websocket初体验(1)

慕雪1434180

2017-12-23 17:52

为啥连接上了,点击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>


写回答 关注

2回答

  • 慕瓜5189485
    2018-11-06 09:49:12

    websocket.onmessage 事件,websocket 是小写 你是大写


  • 西施_
    2017-12-26 15:35:10

    websocket.onmessage 事件,websocket 大写,js 区分大小写的,另外 console 里报错也可以贴出来,有助于快速定位 bug

基于Websocket的火拼俄罗斯(基础)

HTML5火拼俄罗斯基础讲解。

34057 学习 · 57 问题

查看课程

相似问题