提交 90daab05 编写于 作者: B baiy 提交者: ninecents

update

上级 951cd909
<template> <template>
<div> <Row :gutter="10">
<Input v-model="current.url" class="page-option-block" style="margin-bottom: 10px"> <Col span="10">
<Row :gutter="6">
<Col span="18">
<Input v-model="current.url">
<span slot="prepend"> <span slot="prepend">
<Badge style="margin-left:10px" :status="status ? 'success' : 'error'"/> <Badge style="margin-left:10px" :status="status ? 'success' : 'error'"/>
</span> </span>
<Button slot="append" v-if="!status" @click="handle()">{{ $t('websocket_connect') }}</Button>
<Button slot="append" v-else @click="handle()">{{ $t('websocket_close') }}</Button>
</Input> </Input>
<Row :gutter="10"> </Col>
<Col span="10"> <Col span="6">
<input-block top="7px" right="7px"> <Button long v-if="!status" type="success" @click="handle()">连接</Button>
<heightResize :append="['.page-option-block']"> <Button long v-else type="error" @click="handle()">断开</Button>
<autoHeightTextarea v-model="sendContent" :placeholder="$t('websocket_send_content')"/> </Col>
</heightResize> </Row>
<template slot="extra"> <Input style="margin: 10px 0 5px" v-model="sendContent" :rows="14" type="textarea"
<Button :disabled="!status" type="primary" size="small" @click="send">{{ placeholder="发送内容"></Input>
$t('websocket_send') <Button type="primary" @click="send()" long>发送</Button>
}}
</Button>
</template>
</input-block>
</Col> </Col>
<Col span="14"> <Col span="14">
<input-block top="7px" right="7px" :text="$t('websocket_send')" @on-default-right-bottom-click="send">
<heightResize :append="['.page-option-block']" @resize="logHeightSet">
<Card> <Card>
<div class="lists-block" id="log" <p slot="title">交互内容</p>
:style="`height: ${logHeight}px;overflow: hidden;overflow-y:auto;`"> <template slot="extra">
<div v-if="lists.length === 0" style="font-size: 14px;color: #999999"> <Button style="margin-right: 5px" size="small" type="primary" @click="copyAll()">复制全部</Button>
{{ $t('websocket_log_content') }} <Button size="small" type="primary" @click="clear()">清空</Button>
</div> </template>
<div v-else v-for="(item,key) in lists" :key="key">
<div class="lists-block" id="log" style="height: 300px;overflow: hidden;overflow-y:auto;">
<div v-for="(item,key) in lists" :key="key">
<div class="item" v-if="item.type === 'send'" style="color:green"> <div class="item" v-if="item.type === 'send'" style="color:green">
{{ $t('websocket_you') }} {{ item.time }} 你 {{item.time}}
</div> </div>
<div class="item" v-else-if="item.type === 'accept'" style="color:blue"> <div class="item" v-else-if="item.type === 'accept'" style="color:blue">
{{ $t('websocket_server') }} {{ item.time }} 服务端 {{item.time}}
</div> </div>
<div class="item" v-else> <div class="item" v-else>
{{ item.time }} {{item.time}}
</div> </div>
<div class="item"> <div class="item">
<Icon style="cursor: pointer" type="md-copy" @click="copy(item.content)"/> <Icon style="cursor: pointer" type="md-copy" @click="copy(item.content)"/>
{{ item.content }} {{item.content}}
</div> </div>
</div> </div>
</div> </div>
</Card> </Card>
</heightResize>
<template slot="extra">
<Button style="margin-right: 5px" size="small" type="primary" @click="copyAll()">
{{ $t('websocket_copy') }}
</Button>
<Button size="small" type="primary" @click="clear()">{{ $t('websocket_clear') }}</Button>
</template>
</input-block>
</Col> </Col>
</Row> </Row>
</div>
</template> </template>
<script> <script>
import moment from 'moment' import moment from 'moment'
import heightResize from "./components/heightResize";
import autoHeightTextarea from "./components/autoHeightTextarea";
export default { export default {
components: {
heightResize,
autoHeightTextarea
},
created() { created() {
this.$initToolData() this.current = Object.assign(this.current, this.$getToolData())
}, },
methods: { methods: {
handle() { handle() {
...@@ -80,11 +62,6 @@ export default { ...@@ -80,11 +62,6 @@ export default {
return this.connect(); return this.connect();
}, },
connect() { connect() {
if (!this.current.url.trim()) {
return
}
this.$saveToolData(this.current);
this.log(this.$t('websocket_connect_start', [this.current.url]).toString())
let websocket = new WebSocket(this.current.url); let websocket = new WebSocket(this.current.url);
websocket.onopen = (evt) => { websocket.onopen = (evt) => {
this.onOpen(evt) this.onOpen(evt)
...@@ -101,14 +78,13 @@ export default { ...@@ -101,14 +78,13 @@ export default {
this.ws = websocket; this.ws = websocket;
}, },
close() { close() {
this.log(this.$t('websocket_close_start', [this.current.url]).toString())
this.ws.close(); this.ws.close();
}, },
clear() { clear() {
this.lists = []; this.lists = [];
}, },
log(content, type = "other") { log(content, type = "other") {
this.lists.push({content, type, time: moment().format("YYYY-MM-DD HH:mm:ss")}); this.lists.push({content, type,time:moment().format("YYYY-MM-DD HH:mm:ss")});
this.$nextTick(() => { this.$nextTick(() => {
let log = document.getElementById('log'); let log = document.getElementById('log');
log.scrollTop = log.scrollHeight; log.scrollTop = log.scrollHeight;
...@@ -123,52 +99,47 @@ export default { ...@@ -123,52 +99,47 @@ export default {
send() { send() {
try { try {
if (!this.status) { if (!this.status) {
throw new Error(this.$t('websocket_error_connect').toString()) return this.$Message.error("ws还没有连接,或者连接失败,请检测");
} }
if (!this.sendContent) { if (!this.sendContent) {
throw new Error(this.$t('websocket_error_content').toString()) return this.$Message.error("发送内容不能为空");
} }
this.ws.send(this.sendContent); this.ws.send(this.sendContent);
this.log(this.sendContent, 'send') this.log(this.sendContent, 'send')
} catch (e) { } catch (e) {
this.log(this.$t('websocket_error', [e.message]).toString()) this.log('错误异常: ' + e)
} }
}, },
onOpen() { onOpen() {
this.status = true; this.status = true;
this.log(this.$t('websocket_connect_ok').toString()) this.log("连接成功")
}, },
onClose() { onClose() {
this.status = false; this.status = false;
this.log(this.$t('websocket_close_ok').toString()) this.log("连接关闭")
}, },
onMessage(evt) { onMessage(evt) {
this.log(evt.data, 'accept') this.log(evt.data, 'accept')
}, },
onError(evt) { onError(evt) {
this.log(this.$t('websocket_error', [evt.data()]).toString()) this.log('错误异常: ' + evt.data())
},
logHeightSet(height) {
this.logHeight = Math.max(height - 34, 100)
} }
}, },
data() { data() {
return { return {
current: { current: {
url: "wss://echo.websocket.events", url: "wss://echo.websocket.org",
}, },
status: false, status: false,
wx: null, wx: null,
sendContent: "", sendContent: "",
lists: [], lists: [],
logHeight: 100,
} }
}, },
} }
</script> </script>
<style scoped> <style scoped>
.lists-block { .lists-block {
font-size: 14px; font-size: 12px;
line-height: 28px; }
}
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册