• WebSocket介绍及部署


    WebSocket是一种在单个TCP连接上进行全双工通信的协议,其设计的目的是在Web浏览器和Web服务器之间进行实时通信(实时Web)。

    WebSocket协议的优点包括:

    1. 更高效的网络利用率:与HTTP相比,WebSocket的握手只需要一次,之后客户端和服务器端可以直接交换数据;

    2. 实时性更高:WebSocket的双向通信能够实现实时通信,无需等待客户端或服务器端的响应;

    3. 更少的通信量和延迟:WebSocket可以发送二进制数据,而HTTP只能发送文本数据,并且WebSocket的消息头比HTTP更小。

    简单使用示例:

    1. 客户端JavaScript代码:

    1. ```javascript
    2. //创建WebSocket对象
    3. var socket = new WebSocket("ws://localhost:8080/");
    4. //建立连接后回调函数
    5. socket.onopen = function(event) {
    6.     console.log("WebSocket连接建立成功");
    7. };
    8. //接收到消息后回调函数
    9. socket.onmessage = function(event) {
    10.     console.log("接收到消息:" + event.data);
    11. };
    12. //错误回调函数
    13. socket.onerror = function(event) {
    14.     console.log("WebSocket连接发生错误");
    15. };
    16. //关闭回调函数
    17. socket.onclose = function(event) {
    18.     console.log("WebSocket连接关闭");
    19. };
    20. //发送消息
    21. socket.send("hello server");

    2. 服务器端Java代码:

    1. ```java
    2. import java.io.IOException;
    3. import java.net.ServerSocket;
    4. import java.net.Socket;
    5. import java.util.HashSet;
    6. import java.util.Set;
    7. public class WebSocketServer {
    8.     //存储所有连接到服务器的WebSocket对象
    9.     private static Set webSockets = new HashSet<>();
    10.     public static void main(String[] args) throws IOException {
    11.         //创建ServerSocket
    12.         ServerSocket serverSocket = new ServerSocket(8080);
    13.         System.out.println("服务器已启动,监听端口:8080");
    14.         //循环等待客户端连接
    15.         while (true) {
    16.             //创建Socket对象
    17.             Socket socket = serverSocket.accept();
    18.             //创建WebSocket对象,存储到集合中
    19.             WebSocket webSocket = new WebSocket(socket);
    20.             webSockets.add(webSocket);
    21.             //启动线程,处理该WebSocket连接
    22.             new Thread(webSocket).start();
    23.             System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
    24.         }
    25.     }
    26.     //广播消息给所有连接到服务器的WebSocket对象
    27.     public static void broadcast(String message) {
    28.         for (WebSocket webSocket : webSockets) {
    29.             try {
    30.                 webSocket.sendMessage(message);
    31.             } catch (IOException e) {
    32.                 e.printStackTrace();
    33.             }
    34.         }
    35.     }
    36. }

    3. 服务器端WebSocket代码:

    1. ```java
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.net.Socket;
    5. public class WebSocket implements Runnable {
    6.     private Socket socket;
    7.     private InputStream inputStream;
    8.     public WebSocket(Socket socket) throws IOException {
    9.         this.socket = socket;
    10.         this.inputStream = socket.getInputStream();
    11.     }
    12.     //接收消息
    13.     public String receiveMessage() throws IOException {
    14.         byte[] buffer = new byte[1024];
    15.         int len = inputStream.read(buffer);
    16.         return new String(buffer, 0, len);
    17.     }
    18.     //发送消息
    19.     public void sendMessage(String message) throws IOException {
    20.         socket.getOutputStream().write(message.getBytes());
    21.     }
    22.     @Override
    23.     public void run() {
    24.         try {
    25.             while (true) {
    26.                 String message = receiveMessage();
    27.                 System.out.println("接收到消息:" + message);
    28.                 WebSocketServer.broadcast(message);
    29.             }
    30.         } catch (IOException e) {
    31.             e.printStackTrace();
    32.         } finally {
    33.             try {
    34.                 socket.close();
    35.             } catch (IOException e) {
    36.                 e.printStackTrace();
    37.             }
    38.         }
    39.     }
    40. }
  • 相关阅读:
    高项.项目管理经验、理念、教训
    Alma Linux - Primavera P6 EPPM 安装及分享
    IDEA 新建 Maven 项目没有文件结构 pom 文件为空 解决方法
    SpringBoot项目中使用MultipartFile来上传文件(包含多文件)
    Kafka 认证二:ScramLoginModule 认证及 Java 连接测试
    好用的C C++ 日志宏 OutputDebugStringA 写到文件或界面
    C++异常处理throw try catch
    线程安全
    章鱼网络Louis:发现Web3商业价值
    组装式应用为何成为十二大技术趋势
  • 原文地址:https://blog.csdn.net/m0_72196169/article/details/133758081