<!--
@ 作者: 秦东
@ 时间: 2024-05-13 16:10:18
@ 备注: webservice服务
-->
<script lang='ts' setup>
const message = ref('');
const ws = ref(null);

onMounted(() => {
  connect();
});
 
onUnmounted(() => {
  if (ws.value) {
    ws.value.close();
  }
});

function connect() {
	ws.value = new WebSocket('ws://127.0.0.1:17777/ws/websocketDemo');
	
	ws.value.onopen = function(event) {
		console.log('WebSocket 连接已打开', event);
	};
	
	ws.value.onerror = function(error) {
		console.error('WebSocket 出错', error);
	};
	
	ws.value.onmessage = function(event) {
		console.log('收到消息', event.data);
	};
	
	ws.value.onclose = function(err) {
		console.log('WebSocket 连接已关闭',err);
		setTimeout(connect, 3000);
	};
}

function sendMessage() {
  if (ws.value && ws.value.readyState === WebSocket.OPEN) {
    ws.value.send(message.value);
  } else {
    console.error('WebSocket 连接未打开');
  }
}

import Editor from "@/components/WangEditor/index.vue";
const value = ref("初始内容");
</script>
<template>
	<div>
		<input v-model="message" placeholder="输入消息" />
    	<button @click="sendMessage">发送</button>
	</div>

	<div class="app-container">
    <editor v-model="value" style="height: calc(100vh - 124px)" />
  </div>
</template>
<style  lang='scss' scoped>

</style>
