postman连接websocket, 建立连接、聊天测试(v8.5.1)

复制代码
1. postman v8.5版本 以上支持 websocket。
复制代码
2. 选择websocket请求模块
复制代码
File - New...
复制代码
3. WebSocketServer.java 
复制代码
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;


/**
 * 访问: ws://localhost:8080/ws/{userId}
 */
@Component
@ServerEndpoint("/ws/{userId}")
public class WebSocketServer {

    private static int onlineCount = 0;
    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>();
    private Session session;
    private String userId = "";

    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //在线数加1
        this.userId = userId;
        sendMessage(userId + "用户" + ", 连接成功 !");

        System.out.println("【websocket】" + userId + "用户" + "已连接!当前在线人数为" + getOnlineCount());
    }

    @OnClose
    public void onClose() {
        webSocketSet.remove(this);  //从set中删除
        subOnlineCount();           //在线数减1

        System.out.println("【websocket】" + userId +  "用户" +  "已关闭!当前在线人数为" + getOnlineCount());
    }

    @OnMessage
    public void onMessage(String message, Session session) {

        if(message.startsWith("target-")){
            int index = message.indexOf(":");
            String userId = message.substring(7,index);
            sendInfo(message.substring(index + 1), userId);
            return;
        }

        this.session = session;
        sendMessage("【websocket】 服务端收到来自窗口" + userId + "发送的消息:" + message);

    }

    @OnError
    public void onError(Session session, Throwable error) {
        this.session = session;
        error.printStackTrace();
    }

    private void sendMessage(String message) {
        try {
            this.session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 群发消息
    /**
     * 群发自定义消息
     */
    public static void sendInfo(@PathParam("userId") String userId, String message) {
        System.out.println("【websocket】 推送消息给" + userId +  "用户" + ",推送内容:" + message);

        for (WebSocketServer item : webSocketSet) {
            //这里可以设定只推送给这个userId的,为null则全部推送
            if (userId == null) {
//                    item.sendMessage(message);
            } else if (item.userId.equals(userId)) {
                item.sendMessage(message);
            }
        }
    }


    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }

    public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
        return webSocketSet;
    }
}
复制代码
4. postman请求, ws:// 开头
复制代码
ws://localhost:8080/ws/{userId}

⬆️ 是发送的信息, ⬇️ 是接收到的信息

复制代码
userId: 101用户连接
复制代码
userId: 102用户连接
复制代码
userId: 101用户给102用户发消息
复制代码
userId: 102用户给101用户发消息
复制代码
控制台输出:
复制代码
2023-09-12 21:59:37.038  INFO 5172 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-09-12 21:59:37.038  INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-09-12 21:59:37.039  INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
【websocket】101用户已连接!当前在线人数为1
【websocket】102用户已连接!当前在线人数为2
【websocket】 推送消息到给102用户,推送内容:你好, 很高兴认识你; 我是101用户
【websocket】 推送消息到给101用户,推送内容:你好, 我也很高兴认识你; 我是102用户
复制代码
postman v8.5.1版本下载
复制代码
链接: https://pan.baidu.com/s/1CaXKkIFLyluLJd3KNdSlaw 
提取码: dhj5 
相关推荐
游戏开发爱好者814 小时前
iOS重构期调试实战:架构升级中的性能与数据保障策略
websocket·网络协议·tcp/ip·http·网络安全·https·udp
却道天凉_好个秋16 小时前
音视频学习(三十六):websocket协议总结
websocket·音视频
2501_916013741 天前
iOS 多线程导致接口乱序?抓包还原 + 请求调度优化实战
websocket·网络协议·tcp/ip·http·网络安全·https·udp
夏天想1 天前
优化 WebSocket 实现单例连接用于打印【待测试 】
网络·websocket·网络协议
2501_915921432 天前
Fiddler 中文版怎么配合 Postman 与 Wireshark 做多环境接口调试?
websocket·网络协议·tcp/ip·http·网络安全·https·udp
游戏开发爱好者82 天前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106322 天前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
00后程序员张2 天前
免Mac上架实战:全平台iOS App上架流程的工具协作经验
websocket·网络协议·tcp/ip·http·网络安全·https·udp
某公司摸鱼前端3 天前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
2501_915921433 天前
iOS IPA 混淆实测分析:从逆向视角验证加固效果与防护流程
websocket·网络协议·tcp/ip·http·网络安全·https·udp