|
|
|
@ -39,15 +39,15 @@ export default {
|
|
|
|
|
try {
|
|
|
|
|
await axios.post('http://example.com/messages', {
|
|
|
|
|
content: this.newMessage,
|
|
|
|
|
sender: 'Me', // You can replace 'Me' with the sender's name
|
|
|
|
|
sender: 'Me', // 可以将“Me”替换为发件人的姓名
|
|
|
|
|
});
|
|
|
|
|
this.newMessage = ''; // Clear the input field after sending
|
|
|
|
|
// You might want to fetch messages again here to update the UI with the latest messages
|
|
|
|
|
this.newMessage = ''; // 发送后清除输入字段
|
|
|
|
|
// 在此处再次获取消息,以使用最新消息更新 UI
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error sending message:', error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// WebSocket logic to receive messages
|
|
|
|
|
// 用于接收消息的 WebSocket 逻辑
|
|
|
|
|
setupWebSocket() {
|
|
|
|
|
const ws = new WebSocket('ws://example.com/socket');
|
|
|
|
|
ws.onmessage = (event) => {
|
|
|
|
@ -57,8 +57,8 @@ export default {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
created() {
|
|
|
|
|
this.fetchMessages(); // Fetch messages when the component is created
|
|
|
|
|
this.setupWebSocket(); // Setup WebSocket connection
|
|
|
|
|
this.fetchMessages(); // 创建组件时获取消息
|
|
|
|
|
this.setupWebSocket(); // 设置 WebSocket 连接
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|