<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var soc;
if(window.WebSocket) {
soc= new WebSocket("ws://localhost:12345/first");
soc.onmessage = function (ev) {
var res = document.getElementById("res");
res.value = res.value+"\n"+ev.data;
}
soc.onopen = function (ev2) {
alert("onopen");
var res = document.getElementById("res");
res.value = "我连接上了"
}
soc.onclose = function (ev2) {
var res = document.getElementById("res");
res.value = res.value+"\n"+"我已经断开了";
}
}else {
alert("次浏览器不支持")
}
function send(msg) {
if(!window.soc) {//判断socket是否创建成功
return;
}
if(soc.readyState==WebSocket.OPEN) {
soc.send(msg);
}else {
alert("连接还没有建立")
}
}
</script>
<form onsubmit="return false">
<textarea name="msg" style="height: 300px;width: 300px" >
</textarea>
<input type="button" value="发送消息" onclick="send(this.form.msg.value)">
<textarea id="res" style="height: 300px;width: 300px;"></textarea>
<input type="button" value="清空消息" onclick="document.getElementById('res').value=''">
</form>
</body>
</html>
019netty的webiSocket客户端
最新推荐文章于 2024-07-23 09:17:57 发布