49 lines
1.4 KiB
HTML
49 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Collaborative Editing</title>
|
|
<style>
|
|
#editor {
|
|
width: 100%;
|
|
height: 400px;
|
|
border: 1px solid #ccc;
|
|
padding: 10px;
|
|
box-sizing: border-box;
|
|
font-family: monospace;
|
|
}
|
|
</style>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', (event) => {
|
|
const ws = new WebSocket('ws://localhost:6789');
|
|
const editor = document.getElementById('editor');
|
|
|
|
ws.onopen = function(event) {
|
|
console.log('Connection established!');
|
|
};
|
|
|
|
ws.onmessage = function(event) {
|
|
// 当接收到消息时,更新编辑器的内容
|
|
const message = JSON.parse(event.data);
|
|
editor.value = message; // 假设消息直接是编辑内容
|
|
};
|
|
|
|
ws.onclose = function(event) {
|
|
console.log('Connection closed!');
|
|
};
|
|
|
|
editor.oninput = function(event) {
|
|
// 当编辑器内容发生变化时,发送消息
|
|
const message = {
|
|
type: 'edit',
|
|
content: editor.value
|
|
};
|
|
ws.send(JSON.stringify(message));
|
|
};
|
|
});
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<textarea id="editor" placeholder="Start editing..."></textarea>
|
|
</body>
|
|
</html> |