44 Star 221 Fork 74

user_1411900 / Web Socket Helper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
websoket.html 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
user_1411900 提交于 2019-04-22 18:25 . c
<!DOCTYPE html>
<!-- saved from url=(0067)http://demo.sc.chinaz.com/Files/DownLoad/webjs1/201811/jiaoben6322/ -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
list-style-type: none;
}
body {
overflow: hidden;
text-align: center;
padding-top: 30px;
}
</style>
</head>
<body>
<p id="msg"></p>
<hr />
<p id="sub"></p>
<hr />
<script>
// 初始化一个 WebSocket 对象
setInterval(function(){
$.get("http://localhost:59803/api/values/.msg",function(e){ $("#msg").html(JSON.stringify(e)) });
$.get("http://localhost:59803/api/values/.sub",function(e){ $("#sub").html(JSON.stringify(e)) });
},2000);
function loger(val){
$("body").append("<p class='three'>"+val+"</p>");
}
var ws = new WebSocket("ws://localhost:59803/v1/.ws");
loger("初始化一个 WebSocket 对象");
// 建立 web socket 连接成功触发事件
ws.onopen = function() {
loger("open...");
// 使用 send() 方法发送数据
setTimeout(function(){
ws.send("ixpe_userlogin:5e6f340e0baf15689d374bee7838a077");
},2000);
loger("数据发送中...")
};
// 接收服务端数据时触发事件
ws.onmessage = function(evt) {
var received_msg = evt.data;
console.log("数据已接收...");
console.log(received_msg);
loger("数据已接收:"+received_msg);
};
// 断开 web socket 连接成功触发事件
ws.onclose = function() {
loger("连接已关闭...")
};
</script>
</body>
</html>
C#
1
https://gitee.com/magey/Web-Socket-Helper.git
git@gitee.com:magey/Web-Socket-Helper.git
magey
Web-Socket-Helper
Web Socket Helper
master

搜索帮助