Home > Article > Backend Development > WebSocket application scenarios in web applications
Application scenarios of WebSocket in Web applications
WebSocket is a protocol for two-way communication between modern web browsers and servers. Unlike the traditional HTTP protocol, WebSocket allows the server to actively send data to the client without requiring the client to initiate a request. This real-time two-way communication feature makes WebSocket widely used in a variety of Web application scenarios.
// 客户端代码 const socket = new WebSocket('ws://server:port/chat'); socket.onopen = function() { console.log('WebSocket连接已建立'); }; socket.onmessage = function(event) { const message = event.data; console.log('接收到消息:', message); // 处理接收到的消息 }; socket.onclose = function(event) { console.log('WebSocket连接已关闭'); }; document.querySelector('#send-button').addEventListener('click', function() { const message = document.querySelector('#message-input').value; socket.send(message); }); // 服务器端代码(使用Node.js和ws库) const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function(ws) { console.log('已建立WebSocket连接'); ws.on('message', function(message) { console.log('接收到消息:', message); // 处理接收到的消息 // 模拟回复消息 ws.send('收到消息:' + message); }); ws.on('close', function() { console.log('WebSocket连接已关闭'); }); });
// 客户端代码 const socket = new WebSocket('ws://server:port/stock'); socket.onopen = function() { console.log('WebSocket连接已建立'); }; socket.onmessage = function(event) { const stockData = JSON.parse(event.data); console.log('接收到股票数据:', stockData); // 更新展示最新股票行情 }; socket.onclose = function(event) { console.log('WebSocket连接已关闭'); }; // 服务器端代码(使用Node.js和ws库) const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function(ws) { console.log('已建立WebSocket连接'); // 模拟每秒推送一次股票数据 const stockData = { symbol: 'AAPL', price: 150.25, timestamp: Date.now() }; setInterval(function() { ws.send(JSON.stringify(stockData)); }, 1000); ws.on('close', function() { console.log('WebSocket连接已关闭'); }); });
// 客户端代码 const socket = new WebSocket('ws://server:port/editor'); socket.onopen = function() { console.log('WebSocket连接已建立'); }; socket.onmessage = function(event) { const editorData = JSON.parse(event.data); console.log('接收到编辑数据:', editorData); // 更新文档内容 }; socket.onclose = function(event) { console.log('WebSocket连接已关闭'); }; // 用户编辑操作示例(假设使用Quill.js作为富文本编辑器) const editor = new Quill('#editor-container', { theme: 'snow' }); editor.on('text-change', function(delta, oldDelta, source) { if (source === 'user') { const editorData = { delta: delta, timestamp: Date.now() }; socket.send(JSON.stringify(editorData)); } }); // 服务器端代码(使用Node.js和ws库) const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function(ws) { console.log('已建立WebSocket连接'); ws.on('message', function(message) { const editorData = JSON.parse(message); console.log('接收到编辑数据:', editorData); // 处理编辑操作 // 广播编辑操作给其他用户 wss.clients.forEach(function(client) { if (client !== ws && client.readyState === WebSocket.OPEN) { client.send(JSON.stringify(editorData)); } }); }); ws.on('close', function() { console.log('WebSocket连接已关闭'); }); });
Summary:
The emergence of WebSocket has greatly promoted the real-time communication capabilities of Web applications. WebSocket can play a great role in scenarios such as instant chat, real-time data display, and multi-person collaborative editing. Developers can use WebSocket to easily implement these functions and improve user experience and application real-time performance. At the same time, it is worth noting that developers should consider network security and performance issues when using WebSocket to ensure application stability and security.
The above is the detailed content of WebSocket application scenarios in web applications. For more information, please follow other related articles on the PHP Chinese website!