当前位置: 首页 > news >正文

前端WebSocket实时通信实现

在项目中使用WebSocket实现实时通信

WebSocket提供了一种在客户端和服务器之间建立持久连接的方式,可以实现实时数据交换。下面我将展示如何在前端项目中集成WebSocket功能。

设计思路

我将创建一个简单的聊天室界面来演示WebSocket的使用,包含以下功能:

  • 建立WebSocket连接
  • 发送和接收消息
  • 显示连接状态
  • 错误处理和重连机制

实现代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>WebSocket 聊天室</title><style>* {box-sizing: border-box;margin: 0;padding: 0;}body {font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;line-height: 1.6;color: #333;background-color: #f5f7fa;padding: 20px;}.container {max-width: 800px;margin: 0 auto;background: white;border-radius: 10px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);overflow: hidden;}header {background: #4a6ee0;color: white;padding: 20px;text-align: center;}.status-bar {display: flex;justify-content: space-between;align-items: center;padding: 10px 20px;background: #f0f4ff;border-bottom: 1px solid #e0e6ff;}.status {display: flex;align-items: center;}.status-indicator {width: 10px;height: 10px;border-radius: 50%;margin-right: 8px;}.connected {background: #4caf50;}.disconnected {background: #f44336;}.chat-container {padding: 20px;height: 400px;overflow-y: auto;background: #fafbff;}.message {margin-bottom: 15px;padding: 10px 15px;border-radius: 18px;max-width: 80%;word-break: break-word;}.received {background: #e6eeff;border-top-left-radius: 4px;align-self: flex-start;}.sent {background: #4a6ee0;color: white;border-top-right-radius: 4px;margin-left: auto;}.message-input {display: flex;padding: 15px;border-top: 1px solid #e0e6ff;background: #fafbff;}input {flex: 1;padding: 12px 15px;border: 1px solid #d0d9ff;border-radius: 20px;outline: none;font-size: 16px;}button {background: #4a6ee0;color: white;border: none;border-radius: 20px;padding: 12px 20px;margin-left: 10px;cursor: pointer;font-weight: bold;transition: background 0.3s;}button:hover {background: #3a5ec0;}button:disabled {background: #cccccc;cursor: not-allowed;}</style>
</head>
<body><div class="container"><header><h1>WebSocket 聊天室</h1></header><div class="status-bar"><div class="status"><div class="status-indicator disconnected" id="statusIndicator"></div><span id="statusText">未连接</span></div><button id="connectButton">连接</button></div><div class="chat-container" id="chatContainer"><div class="message received">欢迎使用聊天室!请点击"连接"按钮开始。</div></div><div class="message-input"><input type="text" id="messageInput" placeholder="输入消息..." disabled><button id="sendButton" disabled>发送</button></div></div><script>document.addEventListener('DOMContentLoaded', function() {const statusIndicator = document.getElementById('statusIndicator');const statusText = document.getElementById('statusText');const connectButton = document.getElementById('connectButton');const chatContainer = document.getElementById('chatContainer');const messageInput = document.getElementById('messageInput');const sendButton = document.getElementById('sendButton');let websocket = null;const serverUrl = 'wss://echo.websocket.org'; // 这是一个公共的WebSocket测试服务器// 更新连接状态UIfunction updateConnectionStatus(connected) {if (connected) {statusIndicator.className = 'status-indicator connected';statusText.textContent = '已连接';connectButton.textContent = '断开';messageInput.disabled = false;sendButton.disabled = false;} else {statusIndicator.className = 'status-indicator disconnected';statusText.textContent = '未连接';connectButton.textContent = '连接';messageInput.disabled = true;sendButton.disabled = true;}}// 添加消息到聊天窗口function addMessage(content, isSent = false) {const messageElement = document.createElement('div');messageElement.className = `message ${isSent ? 'sent' : 'received'}`;messageElement.textContent = content;chatContainer.appendChild(messageElement);// 滚动到底部chatContainer.scrollTop = chatContainer.scrollHeight;}// 建立WebSocket连接function connect() {try {websocket = new WebSocket(serverUrl);websocket.onopen = function(event) {updateConnectionStatus(true);addMessage('已连接到服务器');};websocket.onmessage = function(event) {addMessage(event.data);};websocket.onclose = function(event) {updateConnectionStatus(false);addMessage('连接已关闭');websocket = null;};websocket.onerror = function(error) {console.error('WebSocket错误:', error);addMessage('发生错误,连接已断开');updateConnectionStatus(false);};} catch (error) {console.error('无法建立连接:', error);addMessage('无法连接到服务器: ' + error.message);updateConnectionStatus(false);}}// 关闭WebSocket连接function disconnect() {if (websocket) {websocket.close();}}// 发送消息function sendMessage() {const message = messageInput.value.trim();if (message && websocket && websocket.readyState === WebSocket.OPEN) {websocket.send(message);addMessage(message, true);messageInput.value = '';}}// 连接/断开按钮点击事件connectButton.addEventListener('click', function() {if (websocket && websocket.readyState === WebSocket.OPEN) {disconnect();} else {connect();}});// 发送按钮点击事件sendButton.addEventListener('click', sendMessage);// 按Enter键发送消息messageInput.addEventListener('keypress', function(event) {if (event.key === 'Enter') {sendMessage();}});});</script>
</body>
</html>

使用说明

  1. 页面加载后,点击"连接"按钮建立WebSocket连接
  2. 连接成功后,可以在输入框中输入消息并点击"发送"或按Enter键
  3. 发送的消息会显示在右侧(蓝色),接收的消息显示在左侧(浅蓝色)
  4. 可以随时点击"断开"按钮结束连接

实际项目中的注意事项

  1. 生产环境URL:示例中使用的是测试服务器,实际项目中应替换为您的WebSocket服务器地址
  2. 安全连接:生产环境应使用wss://(WebSocket Secure)而不是ws://
  3. 错误处理:需要根据实际需求完善错误处理和重连机制
  4. 身份验证:实际应用中通常需要在建立连接时进行身份验证
  5. 数据格式:通常使用JSON格式进行复杂数据交换

扩展功能

在实际项目中,您可能需要添加:

  • 心跳机制保持连接活跃
  • 消息重发机制确保消息送达
  • 连接状态监控和自动重连
  • 支持不同的消息类型(文本、图片、文件等)
  • 用户身份标识和会话管理

文章转载自:

http://l5nuQJJZ.ptqds.cn
http://7xx8lTuO.ptqds.cn
http://qpvHxp6v.ptqds.cn
http://JL1hmw3S.ptqds.cn
http://yZnapFJS.ptqds.cn
http://45nCHp63.ptqds.cn
http://QDNY4yXD.ptqds.cn
http://9MMOtI4I.ptqds.cn
http://b23JfAkG.ptqds.cn
http://YeYvmWnI.ptqds.cn
http://PnIo0468.ptqds.cn
http://WnxoWiaF.ptqds.cn
http://otSWNsKe.ptqds.cn
http://50zgXP4q.ptqds.cn
http://JSeXFx1n.ptqds.cn
http://uPfNkaAY.ptqds.cn
http://f8XX9nqI.ptqds.cn
http://moVnArls.ptqds.cn
http://rDbagCvG.ptqds.cn
http://Szint8By.ptqds.cn
http://0pWFg2wj.ptqds.cn
http://0qLyIrot.ptqds.cn
http://36isiJUa.ptqds.cn
http://TrCZBpJI.ptqds.cn
http://Mqugnmyq.ptqds.cn
http://R3iRzmVN.ptqds.cn
http://WtSOoygq.ptqds.cn
http://yam5WTLk.ptqds.cn
http://ImxN1k8d.ptqds.cn
http://O0GpzpwC.ptqds.cn
http://www.dtcms.com/a/379782.html

相关文章:

  • 2025年- H133-Lc131. 反转字符串(字符串)--Java版
  • 萨顿四条原则
  • NumPy 2.x 完全指南【三十八】伪随机数生成器
  • GitHub 热榜项目 - 日榜(2025-09-12)
  • O3.3 opencv指纹识别
  • 在线会议系统是一个基于Vue3 + Spring Boot的现代化在线会议管理平台,集成了视频会议、实时聊天、AI智能助手等多项先进技术。
  • 每日一算:打家劫舍
  • MemGPT: Towards LLMs as Operating Systems
  • MySQL与PostgreSQL核心区别对比
  • Redis基础命令速查:从连接到数据操作,新手也能上手
  • 信息安全工程师考点-网络安全法律与标准
  • 阿里云OSS vs 腾讯云COS vs AWS S3:对象存储价格与性能深度对比
  • vim复制本地到linux服务器上,换行缩进过大,不对的问题
  • 【贪心算法】day9
  • HarmonyOS 5分布式数据管理初探:实现跨设备数据同步
  • 【Unity UGUI 交互组件——InputFild(TMP版本)(11)】
  • 基于QVTKOpenGLNativeWidget的三维点云可视化实现
  • Qwen3 中注意力机制实现
  • 基于librdkafa C++客户端生产者发送数据失败问题处理#2
  • Maya绑定:渲染编辑器Hypershade简单使用,给小球添加材质纹理
  • 前端基础 —— A / HTML
  • 线性代数 | 行列式与矩阵区别
  • Redis 核心数据结构:String 类型深度解析与 C++ 实战
  • 【Linux】面试常考!Linux 进程核心考点:写时拷贝优化原理 + 进程等待实战,一篇理清进程一生
  • 根据当前门店经纬度,求出1km内的门店
  • java类冲突
  • 线上的Python服务如何部署?
  • ​​Cinema 4D 2026 核心亮点:AI智能驱动 + 无缝实时渲染​
  • 【Pywinauto库】10.7 pywinauto.controls.uia_controls控件
  • Next.js 字体优化:使用 `next/font` 告别布局偏移和性能瓶颈