| | |
| | | |
| | | /** |
| | | * 出现错误 |
| | | * |
| | | * @param session |
| | | * @param error |
| | | */ |
| | | @OnError |
| | | public void onError(Session session, Throwable error) { |
| | |
| | | |
| | | /** |
| | | * 发送消息,实践表明,每次浏览器刷新,session会发生变化。 |
| | | * |
| | | * @param session |
| | | * @param message |
| | | */ |
| | | public static void sendMessage(Session session, String message) { |
| | | try { |
| | | session.getBasicRemote().sendText(String.format("%s", message)); |
| | | // session.getAsyncRemote().sendText(message) |
| | | synchronized (session){ |
| | | session.getBasicRemote().sendText(message); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("发送消息出错:{}", ex.getMessage()); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 群发消息 |
| | | * |
| | | * @param message |
| | | * @throws IOException |
| | | */ |
| | | public static void broadCastInfo(String message) throws IOException { |
| | | for (Session session : SESSION_SET) { |
| | |
| | | |
| | | /** |
| | | * 指定Session发送消息 |
| | | * |
| | | * @param sessionId |
| | | * @param message |
| | | * @throws IOException |
| | | */ |
| | | public static void sendMessage(String message, String sessionId) throws IOException { |
| | | Session session = null; |