1 Star 0 Fork 0

ryderyan/Ryder_JS_Learning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ws.html 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
ryderyan 提交于 2023-06-14 23:21 . ws
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>测试1</div>
<input id="input"/>
<button id="btn">发送</button>
<div id="mess"></div>
</body>
<script>
const mess = document.getElementById("mess")
if(window.WebSocket){
const ws = new WebSocket('ws://localhost:3000','1');
ws.onopen = (e) =>{
console.log("__success_")
}
ws.onclose = (e) =>{
console.log("关闭");
mess.innerHTML = "服务器关闭"
}
ws.onerror = (e)=>{
console.log("ws连接错误",e);
}
ws.onmessage = (e) =>{
let para = document.createElement("p");
let node = document.createTextNode(e.data);
para.appendChild(node);
mess.appendChild(para);
}
document.getElementById("btn").addEventListener("click",(e)=>{
e.preventDefault();
let txt = document.getElementById("input").value;
if(txt != ""){
let sendObj = {
msg:txt,
to:1, }
ws.send( JSON.stringify(sendObj))
}
document.getElementById("input").value = ""
})
}
</script>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryderyan/ryder_-js_-learning.git
git@gitee.com:ryderyan/ryder_-js_-learning.git
ryderyan
ryder_-js_-learning
Ryder_JS_Learning
master

搜索帮助