当前位置:   article > 正文

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

postman连接websocket

1. postman v8.5版本 以上支持 websocket。

2. 选择websocket请求模块
File - New...

3. WebSocketServer.java 

  1. import org.springframework.stereotype.Component;
  2. import javax.websocket.*;
  3. import javax.websocket.server.PathParam;
  4. import javax.websocket.server.ServerEndpoint;
  5. import java.io.IOException;
  6. import java.util.concurrent.CopyOnWriteArraySet;
  7. /**
  8. * 访问: ws://localhost:8080/ws/{userId}
  9. */
  10. @Component
  11. @ServerEndpoint("/ws/{userId}")
  12. public class WebSocketServer {
  13. private static int onlineCount = 0;
  14. private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>();
  15. private Session session;
  16. private String userId = "";
  17. @OnOpen
  18. public void onOpen(Session session, @PathParam("userId") String userId) {
  19. this.session = session;
  20. webSocketSet.add(this); //加入set中
  21. addOnlineCount(); //在线数加1
  22. this.userId = userId;
  23. sendMessage(userId + "用户" + ", 连接成功 !");
  24. System.out.println("【websocket】" + userId + "用户" + "已连接!当前在线人数为" + getOnlineCount());
  25. }
  26. @OnClose
  27. public void onClose() {
  28. webSocketSet.remove(this); //从set中删除
  29. subOnlineCount(); //在线数减1
  30. System.out.println("【websocket】" + userId + "用户" + "已关闭!当前在线人数为" + getOnlineCount());
  31. }
  32. @OnMessage
  33. public void onMessage(String message, Session session) {
  34. if(message.startsWith("target-")){
  35. int index = message.indexOf(":");
  36. String userId = message.substring(7,index);
  37. sendInfo(message.substring(index + 1), userId);
  38. return;
  39. }
  40. this.session = session;
  41. sendMessage("【websocket】 服务端收到来自窗口" + userId + "发送的消息:" + message);
  42. }
  43. @OnError
  44. public void onError(Session session, Throwable error) {
  45. this.session = session;
  46. error.printStackTrace();
  47. }
  48. private void sendMessage(String message) {
  49. try {
  50. this.session.getBasicRemote().sendText(message);
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. // 群发消息
  56. /**
  57. * 群发自定义消息
  58. */
  59. public static void sendInfo(@PathParam("userId") String userId, String message) {
  60. System.out.println("【websocket】 推送消息给" + userId + "用户" + ",推送内容:" + message);
  61. for (WebSocketServer item : webSocketSet) {
  62. //这里可以设定只推送给这个userId的,为null则全部推送
  63. if (userId == null) {
  64. // item.sendMessage(message);
  65. } else if (item.userId.equals(userId)) {
  66. item.sendMessage(message);
  67. }
  68. }
  69. }
  70. public static synchronized int getOnlineCount() {
  71. return onlineCount;
  72. }
  73. public static synchronized void addOnlineCount() {
  74. WebSocketServer.onlineCount++;
  75. }
  76. public static synchronized void subOnlineCount() {
  77. WebSocketServer.onlineCount--;
  78. }
  79. public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
  80. return webSocketSet;
  81. }
  82. }

4. postman请求, ws:// 开头

ws://localhost:8080/ws/{userId}

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

userId: 101用户连接

userId: 102用户连接

 userId: 101用户给102用户发消息

 userId: 102用户给101用户发消息

控制台输出:
  1. 2023-09-12 21:59:37.038 INFO 5172 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  2. 2023-09-12 21:59:37.038 INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  3. 2023-09-12 21:59:37.039 INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  4. 【websocket】101用户已连接!当前在线人数为1
  5. 【websocket】102用户已连接!当前在线人数为2
  6. 【websocket】 推送消息到给102用户,推送内容:你好, 很高兴认识你; 我是101用户
  7. 【websocket】 推送消息到给101用户,推送内容:你好, 我也很高兴认识你; 我是102用户

postman v8.5.1版本下载
  1. 链接: https://pan.baidu.com/s/1CaXKkIFLyluLJd3KNdSlaw
  2. 提取码: dhj5

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/355296
推荐阅读
  

闽ICP备14008679号