Java Code Examples for org.springframework.web.socket.TextMessage#getPayload()

The following examples show how to use org.springframework.web.socket.TextMessage#getPayload() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketServerSockJsSession.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception {
	String payload = message.getPayload();
	if (!StringUtils.hasLength(payload)) {
		return;
	}
	String[] messages;
	try {
		messages = getSockJsServiceConfig().getMessageCodec().decode(payload);
	}
	catch (Throwable ex) {
		logger.error("Broken data received. Terminating WebSocket connection abruptly", ex);
		tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA);
		return;
	}
	if (messages != null) {
		delegateMessages(messages);
	}
}
 
Example 2
Source File: BaseProxyHandler.java    From Jpom with MIT License 6 votes vote down vote up
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
    if (operateLogController == null) {
        operateLogController = SpringUtil.getBean(OperateLogController.class);
    }
    String msg = message.getPayload();
    Map<String, Object> attributes = session.getAttributes();
    ProxySession proxySession = (ProxySession) attributes.get("proxySession");
    JSONObject json = JSONObject.parseObject(msg);
    String op = json.getString("op");
    ConsoleCommandOp consoleCommandOp = ConsoleCommandOp.valueOf(op);
    if (proxySession != null) {
        this.handleTextMessage(attributes, proxySession, json, consoleCommandOp);
    } else {
        this.handleTextMessage(attributes, session, json, consoleCommandOp);
    }
}
 
Example 3
Source File: WebSocketServerSockJsSession.java    From java-technology-stack with MIT License 6 votes vote down vote up
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception {
	String payload = message.getPayload();
	if (StringUtils.isEmpty(payload)) {
		return;
	}
	String[] messages;
	try {
		messages = getSockJsServiceConfig().getMessageCodec().decode(payload);
	}
	catch (Throwable ex) {
		logger.error("Broken data received. Terminating WebSocket connection abruptly", ex);
		tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA);
		return;
	}
	if (messages != null) {
		delegateMessages(messages);
	}
}
 
Example 4
Source File: SocketHandler.java    From SpringBootBucket with MIT License 6 votes vote down vote up
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    logger.info("handleTextMessage start");
    // 将消息进行转化,因为是消息是json数据,可能里面包含了发送给某个人的信息,所以需要用json相关的工具类处理之后再封装成TextMessage,
    // 我这儿并没有做处理,消息的封装格式一般有{from:xxxx,to:xxxxx,msg:xxxxx},来自哪里,发送给谁,什么消息等等
    String msg = message.getPayload();
    logger.info("msg = " + msg);
    WsParam<String> wsParam = JacksonUtil.json2Bean(msg, new TypeReference<WsParam<String>>(){});
    if ("list".equals(wsParam.getMethod())) {
        logger.info("call list method...");
        WsResponse<String> response = new WsResponse<>();
        response.setResult("hello list");
        sendMessageToUser(session, new TextMessage(JacksonUtil.bean2Json(response)));
    }
    logger.info("handleTextMessage end");
    // 给所有用户群发消息
    //sendMessagesToUsers(msg);
    // 给指定用户群发消息
    //sendMessageToUser(userId, msg);
}
 
Example 5
Source File: WebSocketServerSockJsSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void handleMessage(TextMessage message, WebSocketSession wsSession) throws Exception {
	String payload = message.getPayload();
	if (StringUtils.isEmpty(payload)) {
		return;
	}
	String[] messages;
	try {
		messages = getSockJsServiceConfig().getMessageCodec().decode(payload);
	}
	catch (Throwable ex) {
		logger.error("Broken data received. Terminating WebSocket connection abruptly", ex);
		tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA);
		return;
	}
	delegateMessages(messages);
}
 
Example 6
Source File: SnakeWebSocketHandler.java    From spring4ws-demos with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message)
		throws Exception {
	String payload = message.getPayload();
	if ("west".equals(payload)) {
		this.snake.setDirection(Direction.WEST);
	}
	else if ("north".equals(payload)) {
		this.snake.setDirection(Direction.NORTH);
	}
	else if ("east".equals(payload)) {
		this.snake.setDirection(Direction.EAST);
	}
	else if ("south".equals(payload)) {
		this.snake.setDirection(Direction.SOUTH);
	}
}
 
Example 7
Source File: SnakeWebSocketHandler.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    String payload = message.getPayload();
    if ("west".equals(payload)) {
        this.snake.setDirection(Direction.WEST);
    } else if ("north".equals(payload)) {
        this.snake.setDirection(Direction.NORTH);
    } else if ("east".equals(payload)) {
        this.snake.setDirection(Direction.EAST);
    } else if ("south".equals(payload)) {
        this.snake.setDirection(Direction.SOUTH);
    }
}
 
Example 8
Source File: MsfHandler.java    From TrackRay with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    String payload = message.getPayload();
    if (outputStream==null){

        if (flag){
            String response = metasploit.execute(metasploit.list(payload));
            String console = metasploit.getConsole();
            session.sendMessage(Message.NORMAL("c0nso1e##!##"+console));
            session.sendMessage(Message.NORMAL(response));
            while (metasploit.isBusy()) {
                Thread.sleep(1200);
                Map map = metasploit.readResult();
                boolean busy = (boolean) map.get("busy");
                metasploit.setBusy(busy);
                String data = StrUtils.unicodeToString(map.get("data").toString());
                if (StringUtils.isNotEmpty(data)){
                    session.sendMessage(Message.NORMAL(data));
                }
            }
        }

    }else {
        inputWriter.println(payload);
        inputWriter.flush();
    }
    super.handleTextMessage(session, message);
}
 
Example 9
Source File: MyStringWebSocketHandler.java    From SpringAll with MIT License 5 votes vote down vote up
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    // 获取到客户端发送过来的消息
    String receiveMessage = message.getPayload();
    log.info(receiveMessage);
    // 发送消息给客户端
    session.sendMessage(new TextMessage(fakeAi(receiveMessage)));
    // 关闭连接
    // session.close(CloseStatus.NORMAL);
}
 
Example 10
Source File: CustomWebSocketHandler.java    From spring-websocket-template with MIT License 5 votes vote down vote up
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        TextMessage ackMessage = new TextMessage("ACK: " + message.getPayload());
        session.sendMessage(ackMessage);
    } catch (IOException e) {
        logger.warn("Failed to send message to session {}, ignoring quietly", session.getId());
    }
}
 
Example 11
Source File: WebsocketEndPoint.java    From spring-boot-starter-samples with Apache License 2.0 5 votes vote down vote up
@Override  
protected void handleTextMessage(WebSocketSession session,  
        TextMessage message) throws Exception {  
    super.handleTextMessage(session, message);  
    TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");  
    session.sendMessage(returnMessage);  
}
 
Example 12
Source File: ChatWebSocketHandler.java    From mySSM with MIT License 5 votes vote down vote up
@Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) {
//        System.out.println(session.getId()+":send....");
//        chatTextMessageHandler(message.getPayload());
        try {
//            super.handleTextMessage(session, message);
//            System.out.println(session.getId()+" :"+message.getPayload() + "   " + new Date());
            String m = message.getPayload();
            String[] wsk = m.split(",");
            String phone = wsk[0];
            long time = Long.parseLong(wsk[1]);
            String action = wsk[2];
            if (action.equals("start")){
                session.sendMessage(new TextMessage("success"));
                SaveSession.getInstance().save(phone,time);
                return;
            }
            boolean b = SaveSession.getInstance().isHave(phone,time);
            if (b) {
                if (session.isOpen()) {
                    session.sendMessage(new TextMessage("error"));
                }
            } else {
                if (session.isOpen())
                    session.sendMessage(new TextMessage("success"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                session.sendMessage(new TextMessage("error"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
 
Example 13
Source File: RestTemplateXhrTransport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public ResponseEntity<String> executeSendRequestInternal(URI url, HttpHeaders headers, TextMessage message) {
	RequestCallback requestCallback = new XhrRequestCallback(headers, message.getPayload());
	return nonNull(this.restTemplate.execute(url, HttpMethod.POST, requestCallback, textResponseExtractor));
}
 
Example 14
Source File: RestTemplateXhrTransport.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public ResponseEntity<String> executeSendRequestInternal(URI url, HttpHeaders headers, TextMessage message) {
	RequestCallback requestCallback = new XhrRequestCallback(headers, message.getPayload());
	return nonNull(this.restTemplate.execute(url, HttpMethod.POST, requestCallback, textResponseExtractor));
}
 
Example 15
Source File: RedisTextWebSocketHandler.java    From sc-generator with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleTextMessage(final WebSocketSession session, final TextMessage message) throws Exception {
    RxClient rxClient = rxClientMap.get(session.getId());
    if (rxClient == null) {
        session.close();
        return;
    }
    final String command = message.getPayload();
    if (command.equalsIgnoreCase("sync")) {
        sync(session);
    } else {
        String auth = authMap.get(session.getId());

        observableMap.get(session.getId())
                .subscribe(connection -> {
                    if (auth != null) {
                        connection.writeAndFlush("auth " + auth + "\r\n" + command + "\r\n");
                    } else {
                        connection.writeAndFlush(command + "\r\n");
                    }
                    connection.getInput()
                            .distinct()
                            .subscribe(response -> {
                                String requestMessage;
                                if (response instanceof DefaultLastBulkStringRedisContent) {
                                    DefaultLastBulkStringRedisContent content = (DefaultLastBulkStringRedisContent) response;
                                    byte[] bytes = new byte[content.content().readableBytes()];
                                    content.content().readBytes(bytes);
                                    requestMessage = new String(bytes);
                                } else if (response instanceof IntegerRedisMessage) {
                                    requestMessage = String.valueOf(((IntegerRedisMessage) response).value());
                                } else if (response instanceof ErrorRedisMessage) {
                                    requestMessage = ((ErrorRedisMessage) response).content();
                                } else if (response instanceof FullBulkStringRedisMessage) {
                                    requestMessage = "nil";
                                } else if (response instanceof SimpleStringRedisMessage) {
                                    requestMessage = ((SimpleStringRedisMessage) response).content();
                                } else if (response instanceof BulkStringHeaderRedisMessage || response instanceof DefaultBulkStringRedisContent || response instanceof ArrayHeaderRedisMessage) {
                                    return;
                                } else {
                                    requestMessage = response.toString();
                                }

                                try {
                                    session.sendMessage(new TextMessage(requestMessage));
                                } catch (IOException e) {
                                    logger.warn(e.getMessage());
                                }
                            });
                });
    }
}
 
Example 16
Source File: RestTemplateXhrTransport.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseEntity<String> executeSendRequestInternal(URI url, HttpHeaders headers, TextMessage message) {
	RequestCallback requestCallback = new XhrRequestCallback(headers, message.getPayload());
	return this.restTemplate.execute(url, HttpMethod.POST, requestCallback, textResponseExtractor);
}