Răsfoiți Sursa

Merge pull request #183 from sorchk/dev

添加websocket测试工具
Alien-阿烈叔 3 ani în urmă
părinte
comite
12466a4616
3 a modificat fișierele cu 150 adăugiri și 0 ștergeri
  1. 22 0
      apps/websocket/index.css
  2. 53 0
      apps/websocket/index.html
  3. 75 0
      apps/websocket/index.js

+ 22 - 0
apps/websocket/index.css

@@ -0,0 +1,22 @@
+@import url("../static/css/bootstrap.min.css");
+
+html,body {
+    background: #fff;
+}
+.wrapper {
+    width:auto;
+}
+
+.table {
+    font-size: 14px;
+}
+
+.mod-inputs {
+    padding: 20px;
+    background-color: #f5f5f5;
+    border-radius: 6px;
+    border: 1px solid #eee;
+}
+
+
+

+ 53 - 0
apps/websocket/index.html

@@ -0,0 +1,53 @@
+<!DOCTYPE HTML>
+<html lang="zh-CN">
+<head>
+    <title>WebSocket测试</title>
+    <meta charset="UTF-8">
+    <link rel="stylesheet" href="../static/vendor/jquery/jquery-ui.min.css">
+    <link rel="stylesheet" href="index.css"/>
+    <script type="text/javascript" src="../static/vendor/vue/vue.js"></script>
+    <script src="../static/vendor/require/require.js"></script>
+</head>
+<body>
+
+<div class="wrapper" id="pageContainer">
+    <div class="panel panel-default" style="margin-bottom: 0px;">
+        <div class="panel-heading">
+            <h3 class="panel-title">
+                <a href="http://www.baidufe.com/fehelper/feedback.html" target="_blank" class="x-a-high">
+                    <img src="../static/img/fe-16.png" alt="fehelper"/> FeHelper</a>:WebSocket测试</h3>
+        </div>
+    </div>
+    <div class="panel-body">
+        <div class="row mod-inputs">
+            <div class="ui-mt-10">
+                <input type="text" id="url" ref="url" v-model="url" class="form-control"
+                       placeholder="请输入ws地址,例如ws(s)://localhost:8080/ws/single_multi/test/1">
+            </div>
+            <div class="ui-mt-10">
+                <input class="btn btn-sm btn-primary" type="button" :disabled="connBtn" value=" 连  接 "
+                       @click="initWebSocket()">
+                <input class="btn btn-sm btn-danger" type="button" :disabled="closeBtn" value=" 关  闭 "
+                       @click="websocketClose()">
+            </div>
+            <div class="ui-mt-10">
+                <textarea type="text" id="msg" v-model="msg" class="form-control" placeholder="请输入要发送的消息"></textarea>
+            </div>
+            <div class="ui-mt-10">
+                <input class="btn btn-sm btn-success" type="button" :disabled="closeBtn" value="发送消息" @click="websocketSend()">
+                <input class="btn btn-sm btn-warning" type="button" value="清空消息区" @click="cleanupMsg()">
+            </div>
+        </div>
+        <div class="row mod-inputs" style="margin-top: 10px;">
+            <div class="row" v-for="result in results">
+                {{result}}<br/>
+            </div>
+        </div>
+    </div>
+</div>
+<script src="../static/vendor/jquery/jquery-3.3.1.min.js"></script>
+<script src="../static/vendor/jquery/jquery.extend.js"></script>
+<script type="text/javascript" src="../static/vendor/jquery/jquery-ui.min.js"></script>
+<script type="text/javascript" src="index.js"></script>
+</body>
+</html>

+ 75 - 0
apps/websocket/index.js

@@ -0,0 +1,75 @@
+/**
+ * WebSocket测试
+ */
+new Vue({
+    el: '#pageContainer',
+    data: {
+        url: 'ws://localhost:6765/ws/single/test/1',
+        msg: '',
+        connBtn: false,
+        closeBtn: true,
+        results: [],
+        websock: null,
+    },
+    watch: {},
+    mounted: function () {
+        this.$refs.url.focus();
+    },
+    destroyed() {
+        this.websock.close() //离开之后断开websocket连接
+    },
+    methods: {
+        initWebSocket() { //初始化weosocket
+            this.results.push(this.now() + "连接到:" + this.url);
+            console.log(this.now() + "连接到:" + this.url)
+            this.websock = new WebSocket(this.url);
+            this.websock.onmessage = this.websocketOnMessage;
+            this.websock.onopen = this.websocketOnOpen;
+            this.websock.onerror = this.websocketOnError;
+            this.websock.onclose = this.websocketOnClose;
+        },
+        websocketSend() {//数据发送
+            this.websock.send(this.msg);
+            this.results.push(this.now() + "发送:" + this.msg);
+            console.log(this.now() + "发送:" + this.msg)
+            this.msg = '';
+        },
+        websocketClose() {//数据发送
+            this.results.push(this.now() + "关闭连接");
+            console.log(this.now() + "关闭连接")
+            this.websock.close();
+        },
+        cleanupMsg() {
+            this.results = [];
+        },
+        websocketOnOpen(e) { //连接建立之后执行send方法发送数据
+            this.results.push(this.now() + "连接成功");
+            console.log(this.now() + "连接成功!", e)
+            this.connBtn = true
+            this.closeBtn = false
+        },
+        websocketOnError(e) {//连接建立失败重连
+            this.results.push(this.now() + "连接失败,请检查连接地址是否正确或服务器是否正常");
+            console.log(this.now() + "连接失败,请检查连接地址是否正确或服务器是否正常!", e)
+            this.connBtn = false
+            this.closeBtn = true
+        },
+        websocketOnMessage(msg) { //数据接收
+            this.results.push(this.now() + "收到消息:" + msg.data);
+            console.log(this.now() + "收到消息:", msg)
+        },
+        websocketOnClose(e) {  //关闭
+            console.log(this.now() + "连接断开!", e)
+            this.connBtn = false
+            this.closeBtn = true
+        },
+        now() {
+            let date = new Date();
+            return f0(date.getHours()) + ":" + f0(date.getMinutes()) + ":" + f0(date.getSeconds()) + " - ";
+        }
+    }
+
+});
+function f0(i) {
+    return (i < 10 ? '0' : '') + i
+}