|
| 1 | +<template> |
| 2 | + <div class="app-container"> |
| 3 | + <el-form label-width="120px"> |
| 4 | + <el-row type="flex" :gutter="0"> |
| 5 | + <el-col :sm="12"> |
| 6 | + <el-form-item label="WebSocket地址" size="small"> |
| 7 | + <el-input v-model="url" type="text"/> |
| 8 | + </el-form-item> |
| 9 | + </el-col> |
| 10 | + <el-col :offset="1"> |
| 11 | + <el-form-item label="" label-width="0px" size="small"> |
| 12 | + <el-button @click="connect" type="primary" :disabled="ws&&ws.readyState===1"> |
| 13 | + {{ ws && ws.readyState === 1 ? "已连接" : "连接" }} |
| 14 | + </el-button> |
| 15 | + <el-button @click="exit" type="danger">断开</el-button> |
| 16 | + </el-form-item> |
| 17 | + </el-col> |
| 18 | + </el-row> |
| 19 | + <el-form-item label="发送内容" size="small"> |
| 20 | + <el-input type="textarea" v-model="message" :rows="5"/> |
| 21 | + </el-form-item> |
| 22 | + <el-form-item label="" size="small"> |
| 23 | + <el-button type="success" @click="send">发送消息</el-button> |
| 24 | + </el-form-item> |
| 25 | + <el-form-item label="接收内容" size="small"> |
| 26 | + <el-input type="textarea" v-model="content" :rows="12" disabled/> |
| 27 | + </el-form-item> |
| 28 | + <el-form-item label="" size="small"> |
| 29 | + <el-button type="info" @click="content=''">清空消息</el-button> |
| 30 | + </el-form-item> |
| 31 | + </el-form> |
| 32 | + </div> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script> |
| 36 | +import store from "@/store"; |
| 37 | +import {getNowDateTime} from "@/utils/ruoyi"; |
| 38 | +
|
| 39 | +export default { |
| 40 | + data() { |
| 41 | + return { |
| 42 | + url: process.env.VUE_APP_BASE_API + "/websocket/message", |
| 43 | + message: "", |
| 44 | + content: "", |
| 45 | + ws: null, |
| 46 | + }; |
| 47 | + }, |
| 48 | + created() { |
| 49 | + this.url = this.url.replace("http", "ws") |
| 50 | + }, |
| 51 | + methods: { |
| 52 | + connect() { |
| 53 | + if (!'WebSocket' in window) { |
| 54 | + this.$modal.msgError("您的浏览器不支持WebSocket"); |
| 55 | + return; |
| 56 | + } |
| 57 | + const userId = store.getters.userId; |
| 58 | + this.ws = new WebSocket(this.url + "?userId=" + userId); |
| 59 | + const self = this; |
| 60 | + this.ws.onopen = function (event) { |
| 61 | + self.content = self.content + "\n**********************连接开始**********************\n"; |
| 62 | + }; |
| 63 | + this.ws.onmessage = function (event) { |
| 64 | + self.content = self.content + "接收时间:" + getNowDateTime() + "\n" + event.data + "\n"; |
| 65 | + }; |
| 66 | + this.ws.onclose = function (event) { |
| 67 | + self.content = self.content + "**********************连接关闭**********************\n"; |
| 68 | + }; |
| 69 | + this.ws.error = function (event) { |
| 70 | + self.content = self.content + "**********************连接异常**********************\n"; |
| 71 | + }; |
| 72 | + }, |
| 73 | + exit() { |
| 74 | + if (this.ws) { |
| 75 | + this.ws.close(); |
| 76 | + this.ws = null; |
| 77 | + } |
| 78 | + }, |
| 79 | + send() { |
| 80 | + if (!this.ws || this.ws.readyState !== 1) { |
| 81 | + this.$modal.msgError("未连接到服务器"); |
| 82 | + return; |
| 83 | + } |
| 84 | + if (!this.message) { |
| 85 | + this.$modal.msgError("请输入发送内容"); |
| 86 | + return; |
| 87 | + } |
| 88 | + this.ws.send(this.message); |
| 89 | + } |
| 90 | + }, |
| 91 | +}; |
| 92 | +</script> |
0 commit comments