|
webSocket模块(即时通讯)demo示例
[复制链接]
本帖最后由 52yaoer 于 2019-7-13 15:14 编辑
WebSocket用于在Web浏览器和服务器之间进行任意的双向数据传输的一种技术。WebSocket协议基于TCP协议实现,包含初始的握手过程,以及后续的多次数据帧双向传输过程。其目的是在WebSocket应用和WebSocket服务器进行频繁双向通信时,可以使服务器避免打开多个HTTP连接进行工作来节约资源,提高了工作效率和资源利用率。
点击进入模块详情
WebSocketModule.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>Module Develop</title>
- <meta charset="utf-8">
- <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
- <style type="text/css">
- html,
- body {
- height: 100%
- }
- body {
- background-color: #fff;
- margin: 0;
- }
- #wrap {
- height: 100%;
- position: relative;
- }
- #header {
- padding-top: 20px;
- background-color: #5082c2;
- height: 44px;
- position: relative;
- }
- #header h1 {
- font-size: 20px;
- height: 44px;
- line-height: 44px;
- margin: 0em;
- color: #fff;
- margin-left: 100px;
- margin-right: 100px;
- text-align: center;
- }
- #main {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-box-pack: center;
- }
- a.button {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-box-pack: center;
- -webkit-box-align: center;
- height: 32px;
- margin: 8px;
- background-color: rgba(240, 240, 240, 1.0);
- border-color: rgba(220, 220, 220, 1.0);
- border-width: 2px;
- border-style: solid;
- }
- a.active {
- background-color: rgba(240, 240, 240, 0.7);
- }
- </style>
- </head>
- <body>
- <div id="wrap">
- <div id="main">
- <input type="text" class="input1" name="url" id="url" size="40" value="wss://ws**.**:443" />
- <a class="button" tapmode="active" onclick="addEventListener1()">监听消息事件方法</a>
- <a class="button" tapmode="active" onclick="open1()">连接socket</a>
- <a class="button" tapmode="active" onclick="send1()">发送消息</a>
- <a class="button" tapmode="active" onclick="close1()">关闭socket</a>
- <textarea id="deviceid" name="deviceid" cols="30" rows="50"></textarea>
- </div>
- </div>
- </body>
- <script type="text/javascript">
- var demo = null;
- //ws://121.40.165.18:8800
- apiready = function() {
- demo = api.require('webSocket');
- if (!demo) {
- alert("请添加模块后编译");
- return;
- }
- }
- var demo;
- function addEventListener1() {
- demo.addEventListener(function(ret, err) {
- if (ret.status == true && ret.evenType == 'ReturnData') {
- var _deviceid = document.getElementById("deviceid").value;
- document.getElementById("deviceid").value = ret.data + "\n" + _deviceid;
- } else {
- alert(JSON.stringify(ret) + " " + JSON.stringify(err));
- }
- });
- alert("监听设置成功");
- }
- function open1() {
- var _url = document.getElementById("url").value;
- if (_url == '') {
- alert("地址不能为空");
- return;
- }
- demo.open({
- url: _url, //'ws://192.168.1.102:8887/socket'
- pingInterval : 1,
- pingData : 'ping',
- isBtyes : true
- }, function(ret, err) {
- alert(JSON.stringify(ret) + " " + JSON.stringify(err));
- });
- }
- function send1() {
- demo.send({
- data: '我是安卓移动手机',
- isBtyes : true
- }, function(ret, err) {
- alert(JSON.stringify(ret) + " " + JSON.stringify(err));
- });
- }
- function close1() {
- demo.close(function(ret, err) {
- alert(JSON.stringify(ret) + " " + JSON.stringify(err));
- });
- }
- </script>
- </html>
复制代码
|
|