smartboot 开源组织 smartboot 开源组织
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
  • 指南

    • 快速上手
    • 服务配置
    • 静态资源服务
    • 请求路由
    • 安全性设计
    • WebSocket
      • 关于 smart-http-restful
    目录

    WebSocket

    # WebSocket

    采用 smart-http 进行 websocket 开发与普通 http 请求差别不大, Http 开发涉及到的接口在 WebSocket 中都有与之相对应的实现,见下表。

    差异 Http WebSocket
    请求 HttpRequest WebSocketRequest
    响应 HttpResponse WebSocketResponse
    路由 HttpRouteHandle WebSocketRouteHandle

    # 示例

    public class WebSocketDemo {
        public static void main(String[] args) {
            //1. 实例化路由Handle
            WebSocketRouteHandler routeHandle = new WebSocketRouteHandler();
          
            //2. 指定路由规则以及请求的处理实现
            routeHandle.route("/", new WebSocketDefaultHandler() {
                @Override
                public void handleTextMessage(WebSocketRequest request, WebSocketResponse response, String data) {
                 response.sendTextMessage("接受到客户端消息:" + data);
                }
            });
          
            // 3. 启动服务
            HttpBootstrap bootstrap = new HttpBootstrap();
            bootstrap.webSocketHandler(routeHandle);
            bootstrap.start();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    运行上述代码后,可以通过 websocket 客户端建立连接并调试接口。此处推荐:在线测试 (opens new window)

    安全性设计
    关于 smart-http-restful

    ← 安全性设计 关于 smart-http-restful→

    Theme by Vdoing | Copyright © 2017-2025 三刀
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式