Websocket(4)处理类
5、处理类
public class MyWebSocketHandler extends TextWebSocketHandler { protected Log log = Log.getInstance(MyWebSocketHandler.class); private static final ArrayList<WebSocketSession> users = new ArrayList<WebSocketSession>(); @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { System.out.println("进入了工具类"); System.out.println("接收数据:"+message.getPayload().toString()); // 处理消息 msgContent消息内容 TextMessage textMessage = new TextMessage("abc"); session.sendMessage(textMessage); } @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { log.info("connect websocket success......."); users.add(session); } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // TODO Auto-generated method stub } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // TODO Auto-generated method stub log.info("connect websocket closed......."); users.remove(session); } // 给所有用户发送 信息 public void sendMsgToAllUsers(WebSocketMessage<?> message) throws Exception{ for (WebSocketSession user : users) { user.sendMessage(message); } } protected void handlePingMessage(WebSocketSession session) throws Exception { byte[] array = new byte[1]; array[0] = 1; ByteBuffer buffer = ByteBuffer.wrap(array); PingMessage pingMessage = new PingMessage(buffer); session.sendMessage(pingMessage); } }