Cloudhopper SMPP - How to send MO sms to client?

2019-09-17 07:12发布

问题:

Demo from https://github.com/twitter/cloudhopper-smpp/blob/master/src/test/java/com/cloudhopper/smpp/demo/ServerMain.java provides an example of receiving MT messages from client. But how to send messages (deliver_sm) from server to connected client?

回答1:

I just took code from ClientMain and changed SubmitSm to DeliverSm. For testing purpose I read msg text from Console and send Message using this code. You should call this method in separate thread. Also you should handle list of sessions so you can get session reference when you want to send message.

void sendMessage( SmppSession session,  String message) {

    try {
        byte[] textBytes = CharsetUtil.encode(message, CharsetUtil.CHARSET_UCS_2);

        DeliverSm deliver = new DeliverSm();

        deliver.setSourceAddress(new Address((byte)0x03, (byte)0x00, "40404"));
        deliver.setDestAddress(new Address((byte)0x01, (byte)0x01, "44555519205"));
        deliver.setShortMessage(textBytes);

        WindowFuture<Integer,PduRequest,PduResponse> future = session.sendRequestPdu(deliver, 10000, false);
        if (!future.await()) {
            logger.error("Failed to receive deliver_sm_resp within specified time");
        } else if (future.isSuccess()) {
            DeliverSmResp deliverSmResp = (DeliverSmResp)future0.getResponse();
            logger.info("deliver_sm_resp: commandStatus [" + deliverSmResp.getCommandStatus() + "=" + deliverSmResp.getResultMessage() + "]");
        } else {
            logger.error("Failed to properly receive deliver_sm_resp: " + future.getCause());
        }
    } catch (Exception e) {}
}


标签: java smpp