vue和websocket实现在线聊天,画出自己聊天框和他人聊天框样式,详细代码

时间: 2024-03-23 21:03:37 浏览: 11
好的,这是一个比较完整的 Vue.js + WebSocket 实现在线聊天的例子,包括样式和代码: 首先,需要引入 Vue.js 和 WebSocket 相关的库: ```html <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.9.2/qs.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script> ``` 然后,在 Vue 实例中定义聊天室的组件: ```html <template> <div class="chat-room"> <div class="chat-box"> <div class="chat-history"> <div v-for="(message, index) in messages" :key="index"> <div :class="{'my-message': message.isMine, 'other-message': !message.isMine}"> <div class="message-text">{{ message.text }}</div> <div class="message-time">{{ message.time }}</div> </div> </div> </div> <div class="chat-input"> <input v-model="inputText" @keydown.enter="sendMessage" placeholder="输入消息..."> <button @click="sendMessage">发送</button> </div> </div> </div> </template> <script> export default { data() { return { messages: [], inputText: '' } }, created() { // 初始化 WebSocket 连接 this.socket = io('ws://localhost:3000') this.socket.on('message', (data) => { this.messages.push({ text: data.message, time: data.time, isMine: false }) }) }, methods: { sendMessage() { if (this.inputText.trim() === '') { return } this.messages.push({ text: this.inputText, time: new Date().toLocaleTimeString(), isMine: true }) this.socket.emit('message', { message: this.inputText, time: new Date().toLocaleTimeString() }) this.inputText = '' } } } </script> <style scoped> .chat-room { display: flex; justify-content: center; align-items: center; height: 100vh; } .chat-box { display: flex; flex-direction: column; width: 400px; height: 600px; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; } .chat-history { flex: 1; padding: 10px; overflow-y: auto; } .my-message { display: flex; flex-direction: row-reverse; } .other-message { display: flex; flex-direction: row; } .message-text { min-width: 80px; max-width: 200px; background-color: #eee; padding: 5px; border-radius: 5px; margin: 5px; } .message-time { font-size: 10px; color: #999; margin: 5px; } .chat-input { display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f9f9f9; border-top: 1px solid #ccc; } .chat-input input { flex: 1; height: 30px; border-radius: 5px; padding: 5px; font-size: 14px; outline: none; border: none; } .chat-input button { height: 30px; width: 80px; border-radius: 5px; background-color: #3f51b5; color: #fff; border: none; cursor: pointer; font-size: 14px; } </style> ``` 最后,在 Vue 的路由中添加聊天室的路径: ```javascript const routes = [ { path: '/chat', name: 'ChatRoom', component: ChatRoom } ] ``` 这样,就可以通过访问 `/chat` 路径来进入聊天室页面了。当用户在输入框中输入消息并发送时,页面会实时更新并将消息发送给服务器,其他用户也能够实时收到并显示在页面上。 需要注意的是,这里假设服务器已经部署好,并且使用 WebSocket 来实现消息的实时传输。如果你需要在本地测试,可以使用 `socket.io` 库来模拟 WebSocket 服务器,并且需要将 `ws://localhost:3000` 替换为你本地服务器的地址。

相关推荐

最新推荐

recommend-type

基于vue和websocket的多人在线聊天室

主要介绍了基于vue和websocket的多人在线聊天室,需要的朋友可以参考下
recommend-type

WebSocket实现简单客服聊天系统

主要为大家详细介绍了WebSocket实现简单客服聊天系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

springboot+vue实现websocket配置过程解析

主要介绍了springboot+vue实现websocket配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Vue通过WebSocket建立长连接的实现代码

主要介绍了Vue通过WebSocket建立长连接的实现代码,文中给出了问题及解决方案,需要的朋友可以参考下
recommend-type

Vue实现鼠标经过文字显示悬浮框效果的示例代码

主要介绍了Vue实现鼠标经过文字显示悬浮框效果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。