1 Star 0 Fork 74

xweb / Web Socket Helper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
websoket-service.html 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
user_1411900 提交于 2019-04-25 16:26 . p
<!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>
var service = 'https://api.itool.store/';
setInterval(function() {
$.get(service + "api/values/.msg", function(e) {
$("#msg").html(JSON.stringify(e))
});
$.get(service + "api/values/.sub", function(e) {
$("#sub").html(JSON.stringify(e))
});
}, 2000);
function loger(val) {
$("body").append("<p class='three'>" + val + "</p>");
}
var ajax = {
post: function(url,data,call) {
$.ajax({
url: url,
type: "post",
dataType: "json",
data: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
},
success: call
});
}
};
loger("根据客户端ID 获取 登陆令牌 token");
ajax.post(service + '/.v1/socket/user/.wsbyclient', {
"uuid": "123",
"projectToken": "6D7-BB91-13901823A235",
"parameter": JSON.stringify({
userName: "哈哈",
headImg:"http://9echem.com/public/imgs/banner_binghua.png",
age: 28
}),
"url": "http://www.baidu.com"
}, function(e) {
if (e.code) {
loger("已获取 登陆令牌:" + e.info);
var ws = new WebSocket("wss://api.itool.store/v1/.ws");
loger("初始化一个 WebSocket 对象");
// 建立 web socket 连接成功触发事件
ws.onopen = function() {
loger("open...");
// 使用 send() 方法发送数据
setTimeout(function() {
ws.send("ixpe_userlogin:" + e.info);
}, 2000);
loger("数据发送中...")
};
// 接收服务端数据时触发事件
ws.onmessage = function(evt) {
loger("数据已接收:" + evt.data);
var received_msg = JSON.parse(evt.data);
console.log(received_msg);
};
// 断开 web socket 连接成功触发事件
ws.onclose = function() {
loger("连接已关闭...")
};
} else {
console.log(e.info)
}
});
</script>
</body>
</html>
C#
1
https://gitee.com/xweb/Web-Socket-Helper.git
git@gitee.com:xweb/Web-Socket-Helper.git
xweb
Web-Socket-Helper
Web Socket Helper
master

搜索帮助